简体   繁体   English

正则表达式javascript搜索单词,但忽略几个单词

[英]Regex javascript search word but ignore couple of words

Good afternoon, 下午好,

Would like to know if it is possible to search a word but ignore the same word with other word in regex. 想知道是否可以搜索一个单词,但忽略正则表达式中与另一个单词相同的单词。

Example : One : Two : Three : Four : Five : Six : Two Three : Nine 示例:一:二:三:四:五:六:二三:九

Answer : One : Two : Three : Four : Five : Six : Two Three : Nine 答案:一:二: :四:五:六:二三:九

So the regex only found the first word Three and ignore Two Three. 因此,正则表达式仅找到第一个单词Three,而忽略Two Three。

I tried this regex : 我尝试了这个正则表达式:

(?!Two Three)(Three)

but it doesn't work, it does work if the word is two 但它不起作用,如果单词是2则它起作用

Any help or suggestions i'd gladly appreciate. 任何帮助或建议,我都将非常感谢。 Thanks 谢谢

With the input One : Two : Three : Four : Five : Six : Two Three : Nine 使用输入One : Two : Three : Four : Five : Six : Two Three : Nine

in which you want to ignore Two Three you can do 您可以忽略其中的Two Three

let str = 'One : Two : Three : Four : Five : Six : Two Three : Nine';
let re = /Two(?!\sThree)/g;
console.log(str.match(re));

If you are searching for something more generic I suggest a pure js approach: 如果您正在寻找更通用的东西,我建议使用纯js方法:

 let str = 'One : Two : Three : Four : Five : Six : Two Three : Nine'; let resp = str .split(' : ') .reduce((acc, ele) => acc.some(x => ele.search(x) > 0) ? acc : acc.concat(ele), []) .join(' : '); console.log(resp); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM