admin 管理员组

文章数量: 1184232

最近面试一个前端岗位,出了一个面试题。
问题:给定任意英文段落,段落内可能包含多组单引号组合,要求将单引号组合中的左单引号字符 替换为 ` ,右单引号字符 保持不变?

实现方式:

<!DOCTYPE html><htmllang="zh_cn"><head><metacharset="UTF-8"><title>Title</title><scripttype="text/javascript"src="js/vue.min.js"></script></head><body><div><textareacols="120"rows="10"id="inputText"></textarea><buttonid="replaceButton">替换单引号</button></div><div><h1>替换结果:</h1><pid="result"></p></div></body><scripttype="text/javascript">
    window.onload=function(){
        document.getElementById("replaceButton").addEventListener("click",this.replaceMsg,false);};functionreplaceMsg(){var forReplace = document.getElementById("inputText").value;if(forReplace ===""|| forReplace ===null|| forReplace === undefined){alert("请输入英文字段");return;}var strArray = forReplace.split(" ");var result ="";for(var i =0; i < strArray.length ; i ++){if(strArray[i].indexOf("'")!==-1){if(/\w/.test(strArray[i].substring(0,(strArray[i].indexOf("'"))))&&/\w/.test(strArray[i].substring((strArray[i].indexOf("'")+1)))){
                    result += strArray[i]+" ";}else{if(strArray[i].substring(0,(strArray[i].indexOf("'")))===""){// 表示字符的开始
                        result +="`"+ strArray[i].substring(strArray[i].indexOf("'")+1)+" ";}else{if(strArray[i].substring(strArray[i].indexOf("'")+1)===""){
                            result += strArray[i]+" ";}}}}else{
                result += strArray[i]+" ";}}
        document.getElementById("result").innerHTML = result;}</script></html>

本文标签: 系统 编程 掌握如何