繁体   English   中英

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

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

下午好,

想知道是否可以搜索一个单词,但忽略正则表达式中与另一个单词相同的单词。

示例:一:二:三:四:五:六:二三:九

答案:一:二: :四:五:六:二三:九

因此,正则表达式仅找到第一个单词Three,而忽略Two Three。

我尝试了这个正则表达式:

(?!Two Three)(Three)

但它不起作用,如果单词是2则它起作用

任何帮助或建议,我都将非常感谢。 谢谢

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

您可以忽略其中的Two Three

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

如果您正在寻找更通用的东西,我建议使用纯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