繁体   English   中英

正则表达式匹配精确单词不在其他字符之前或之后

[英]Regex match exact word not preceded or followed by other characters

我试图制作一个匹配一组单词的正则表达式。

例如,如果我匹配一组单词 - American Tea

然后在字符串中American Tea is awesome. Do you like American Tea? love WowAmerican Tea #American Tea American Tea is awesome. Do you like American Tea? love WowAmerican Tea #American Tea American Tea is awesome. Do you like American Tea? love WowAmerican Tea #American Tea ,这里只有2场比赛,

' 美国茶很棒。 你喜欢美国茶吗? 喜欢WowAmerican Tea #American Tea'

所以,我试图只做单词集的完整匹配。

我尝试了一些方法,但没有得到正确的正则表达式:(如果有人可以提供帮助或者可以指出我的方向,那将是非常有帮助的。

检查一下

'American Tea lalalal qwqwqw American Tea sdsdsd #American Tea'.match(/(?:^|\\s)(American Tea)(?=\\s|$)/g)

结果是["American Tea", " American Tea"]

我不希望第二场比赛的空间,我希望比赛结果是["American Tea", "American Tea"]

(第二届美国茶叶前没有空间)

使用.replace()获得乐趣和利润

/(?:^|\s)(american tea)/ig

https://regex101.com/r/qB0uO2/1

如果你想考虑前缀后缀:

/(?:^|\s)(american tea)(?:\W|$)/ig 

https://regex101.com/r/qB0uO2/2

JSBIN示例

var str = "American Tea is awesome. Do you like American Tea? love WowAmerican Tea #American Tea";

str.replace(/(?:^|\s)(american tea)(?:\W|$)/ig, function(i, m){
  console.log(m);
});

//"American Tea"
//"American Tea"

编辑:

上面只返回匹配项,如果您希望保留捕获和匹配前缀,后缀使用capture-groups以及它们

 var str = "American Tea is awesome. Do you like American Tea? love WowAmerican Tea #American Tea"; var newStr = str.replace(/(^|\\s)(american tea)(\\W|$)/ig, function(im, p1, p2, p3){ return p1 +"<b>"+ p2 +"</b>"+ p3; // p1 and p3 will help preserve the pref/suffix }); document.getElementById("result").innerHTML = newStr; 
 <div id="result"></div> 

其中p艺术

  • p1是第一个匹配组(任何前缀)
  • p2是第二个匹配组(“美国茶”字)
  • p3是第三个匹配组(任何后缀)

阅读评论我意识到正则表达式可能不是最好的解决方案。 然而,你是如何避免Javascript不支持一个积极的lookbehind这一事实,这将使这项任务变得容易。

如果JS有(?<= ...)构造,那么你只需使用一个正面的lookbehind和一个正面的向前看,并列出你想要允许在American Tea左右两侧的所有字符。 所以我们想要的是这样的:

(?<=\s|\.|,|:|;|\?|\!|^)American Tea(?=\s|\.|,|:|;|\?|\!|$)

在左侧,您将允许任何列出的字符和字符串^的开头。 在右侧,您允许相同的字符和字符串$的结尾。

但是Javascript没有(?<= ...)构造。 所以我们必须有点创意:

(?=(\s|\.|,|:|;|\?|\!|^))\1(American Tea)(?=\s|\.|,|:|;|\?|\!|$)

这个正则表达式以积极的前瞻取代积极的外观。 然后它与前瞻中发现的任何东西匹配\\ 1,最后美国茶将会捕获第1组。

演示: https//regex101.com/r/qX9qR3/3

你不需要正则表达式匹配单词。

我知道一个非常巧妙的CoffeeScript片段:

wordList = ["coffeescript", "eko", "talking", "play framework", "and stuff", "falsy"]
tweet = "This is an example tweet talking about javascript and stuff."

wordList.some (word) -> ~tweet.indexOf word # returns true

其中编译成以下javascript:

var tweet, wordList;

wordList = ["coffeescript", "eko", "talking", "play framework", "and stuff", "falsy"];

tweet = "This is an example tweet talking about javascript and stuff.";

wordList.some(function(word) { // returns true
  return ~tweet.indexOf(word); 
});

〜不是CoffeeScript中的特殊操作符,只是一个很酷的技巧。 它是按位NOT运算符,它反转其操作数的位。 在实践中,它等同于-x-1。 这里它的工作原理是我们要检查大于-1的索引,并且 - ( - 1)-1 == 0的计算结果为false。

如果您想要匹配的单词,请使用:

wordList.filter (word) -> ~tweet.indexOf word # returns : [ "talking", "and stuff" ]

或者在JS中也是如此:

wordList.filter(function(word) { // returns : [ "talking", "and stuff" ]
  return ~tweet.indexOf(word);
});

虽然杰里米当然是对的,但我认为你的问题比你人为的例子更明显。

从它看起来你正试图有常规的RegEx单词边界,除了你认为单词字符的“#”部分。 在这种情况下,您可以执行以下操作:(其中\\ b表示“单词边界”)

(^|[^#])\bAmerican Tea\b

或者,如果您只想列出您认为非单词字符的字符,则可以执行以下操作来模拟单词边界:

(^|[^A-Za-z])American Tea($|[^A-Za-z])

你可以在http://www.regexr.com/上玩游戏

暂无
暂无

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

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