Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript样式特殊字符之间的所有单词_Javascript_Regex - Fatal编程技术网

javascript样式特殊字符之间的所有单词

javascript样式特殊字符之间的所有单词,javascript,regex,Javascript,Regex,我正在寻找样式的字符串中包含特殊字符的一些字。 例如,如果我有 “我想用单引号'blue'和方括号[red]来设置单词的样式。字符串中有多个'blue'和[red]单词” 我希望它看起来像这样,没有特殊的字符 “我想用蓝色单引号和红色方括号来设置单词的样式…” 我如何用下面的方法将单个报价的结尾替换为 这就是我目前所拥有的 highlightMessage(message) { var text = message.replace(/[\[']+/g, "<span class='

我正在寻找样式的字符串中包含特殊字符的一些字。 例如,如果我有

“我想用单引号'blue'和方括号[red]来设置单词的样式。字符串中有多个'blue'和[red]单词”

我希望它看起来像这样,没有特殊的字符

“我想用蓝色单引号和红色方括号来设置单词的样式…”

我如何用下面的方法将单个报价的结尾替换为

这就是我目前所拥有的

highlightMessage(message) {
    var text = message.replace(/[\[']+/g, "<span class='red'>")
    var text = message.replace(/]\]']+/g, "</span")
    var text = message.replace(/'/g, "<span class='blue'>")
    //how can I replace the end of the single quote with </span>
    //the method above replaces it with <span class='blue'>

    return message;
}
highlightMessage(消息){
var text=message.replace(/[\[']+/g,”)

var text=message.replace(/]\]']+/g,“您可以执行单个
replace
操作以实现所需:

var s=“我想用单引号'blue'和方括号[red]来设置单词的样式。字符串中有多个'blue'和[red]单词”;
var res=s.replace(/'([^']+)'|\[([^\][]+)]]/g,“$1$2”)

console.log(res);
哇,太快了。你必须每分钟输入300个字符,因为我在同一时间内写了大约一半的字符span+1