match string with comma javascript
本问题已经有最佳答案,请猛点这里访问。
我想匹配带或不带逗号和空格的字符串:
1 2 3 4 5 6 7 8 9 10 11 | "hello world" "hello,world" "hello world," ",hello world" ",hello,world," ", hello , world ," |
所以,如果我搜索"world"这个例子,我想用逗号或不带空格来查找它,并突出显示结果。
1 2 3 4 5 6 7 8 | var search ="world"; //or var search ="world,"; //or var search =",world"; //or var search =",world,"; div.replace(new RegExp('(' + search + ')', 'gi'), '<span class="highlighted">$1</span>') |
号
这样地:
1 2 3 4 5 6 7 8 9 10 11 | "hello <span class="highlighted">world</span>" "hello<span class="highlighted">,world</span>" "hello <span class="highlighted">world,</span>" ",hello <span class="highlighted">world</span>" ",hello<span class="highlighted">,world,</span>" ", hello <span class="highlighted">, world ,</span>" |
下面是我想要的一个例子:https://jsfiddle.net/nvdhqf23/5/
多亏了tmkeleher我才解决了https://jsfiddle.net/nvdhqf23/6/
1 | /([\s,]\s*world\s*,?)/i |
。
这应该处理更换。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | <style> .highlighted { background-color: rgb(100,100,255); color: white; } </style> "hello world" "hello,world" "hello world," ",hello world" ",hello,world," ", hello , world ," var content = document.getElementById("0").innerHTML.split(""), result = []; for(var i in content) { // First extract what we want. var world = /([\s,]\s*world\s*,?)/.exec(content[i])[1], // Split everything up to make the insertion easier. hello = content[i].split(world); // Place the result back. result.push(hello.join('<span class="highlighted">'+world+'</span>')); } document.getElementById("1").innerHTML = result.join(""); |
更新以适应@jsem在评论中的要求,我创建了一个函数来确定正确的短语,例如"hello world"或其他许多基于搜索和标点符号的短语。
1 2 3 4 5 6 | function separate(search, punc) { if(typeof punc !=="string") punc =","; var phrase = new RegExp(".*?(\\w+)\\s*(?:"+punc+"|\\s)\\s*(\\w+)\\s*(?:"+punc+")?","i").exec(search); if(!phrase) throw new Error("Search passed could not be parsed."); return {word1:phrase[1], word2:phrase[2]}; }; |
。
然后使用从中收集的信息创建一个唯一的正则表达式,该表达式将捕获要突出显示的信息。
1 2 3 4 | function build_regex(phrase, punc) { if(typeof punc !=="string") punc =","; return new RegExp("^.*?"+phrase.word1+"\\s*((?:"+punc+"|\\s)\\s*"+phrase.word2+"\\s*(?:"+punc+")?).*$","i"); }; |
。
使用这两个函数,我创建了一个突出显示函数,使用与以前相同的分割和连接算法。
1 2 3 4 5 6 | function highlight(sentence, search, punc) { if(typeof punc !=="string") punc =","; var highlighted = build_regex(separate(search, punc), punc).exec(sentence)[1], remains = sentence.split(highlighted); return remains.join('<span class="highlighted">'+highlighted+'</span>'); }; |
前任:
1 2 3 4 5 6 | highlight("This is my sentence hello, world!","hello world"); /* Output: This is my sentence hello<span class="highlighted">, world</span>!*/ highlight("Change things up... sir.","up sir","\\.\\.\\."); /* Output: Change things up<span class="highlighted">... sir</span>.*/ highlight("Give me some options? ummm...","options! ummm","\\?|\\!"); /* Output: Give me some options<span class="highlighted">? ummm</span>...*/ |
。
为您的regex尝试此操作:
1 | var re = /([,|\s]+)?world([,|\s]+)?/g; |
只需在
1 | var search =",?world,?"; |
号
如果你真的想匹配空格(黑体?!),只需添加
1 | div.replace(new RegExp('(\\s*' + search + '\\s*)', 'gi'), ... |
希望有帮助。