通过Javascript正则表达式选择所有不是特定单词的单词

Select all words that are not a specific word, via Javascript regex?

本文关键字:单词 Javascript 正则表达式 选择 通过      更新时间:2023-09-26

我想选择除单词:'Lorem'以外的每个单词。

I have try:

^((?!Lorem).*)$ //nothing is selected.
^((?!test).*)$ //entire paragraph is selected.

: 神的恩赐,神的恩赐,神的恩赐。爱,就是爱,就是爱,就是爱,就是爱,就是爱。秃鹫,只对一tellus scerisque viverra。Morbi的意思是"我不知道"。没有凹痕,在调味品等处有凹痕,没有凹痕。芦笋、芦笋、芦笋、芦笋、芦笋、芦笋、芦笋。菜豆状花序明显,三棱状花序不明显,花叶状花序明显。整型颗粒状结构,非整数型颗粒状结构。孕妇无孕,孕妇无孕,孕妇无孕。熔体基质的结构与lORem的融合。在tellus发酵菌中,前庭的毛囊和前庭的毛囊被发现。现在我要把我的爱情告诉你。车腔炎,无腔炎。无病无病,无病无病,无叶无病。Curabitur eu tristique risus。舌状赘肉,舌状赘肉,舌状赘肉,舌状赘肉。在中间设施中。Nullam non - congue felis。前庭在自由与自由之间的关系。[p] [p] [p] [p]

测试: http://regexpal.com/

这将获取文本中除 Lorem以外的所有单词。

result = subject.match(/'b(?!Lorem'b)'w+'b/g);

不确定这是不是你要找的!

解释:

// 'b(?!'bLorem'b)'w+'b
// 
//    Assert position at a word boundary «'b»
//    Assert that it is impossible to match the regex below starting at this position (negative lookahead) «(?!'bLorem'b)»
//    Match the characters “Lorem” literally «Lorem»
//    Assert position at a word boundary «'b»
//    Match a single character that is a “word character” (letters, digits, etc.) «'w+»
//    Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
//    Assert position at a word boundary «'b»

你是说你想创建一个段落的副本,删除不恰当的词吗?为此,您可以使用:

var filtered = paragraph.replace(/'bLorem'b/g, '');

根据下面的注释更新:哦,所以你想要:

var filtered = paragraph.replace(/'b(?!Lorem'b)('w+)/g, 'matched word:$1');

如果你必须使用正则表达式,你可以使用这个:

/(?!lorem)'b'w+/gi

不需要正则表达式:

s.split('Lorem').join('');

当然,对于不区分大小写的版本,您需要一个:

s.split(/Lorem/i).join('');

如果通过"select"你的意思是你想要一个除lorem以外的所有单词的数组,取上面的输出并在单词边界上分割,例如's+或'b。