简体   繁体   English

匹配以特殊字符开头或结尾的整个单词

[英]Matching whole words that start or end with special characters

I need a regular expression in javascript that matches whole words that start or end with special characters?我需要一个 javascript 中的正则表达式来匹配以特殊字符开头或结尾的整个单词?

It was supposed to be easy, but for some reason \\b after ?这应该很容易,但由于某种原因\\b之后? doesn't behave as I expected:不符合我的预期:

> /FOO\?/.exec('FOO? ')
[ 'FOO?', index: 0, input: 'FOO? ', groups: undefined ]
> /FOO\?\b/.exec('FOO? ')
null

What I need, for instance if my word is "FOO?"我需要什么,例如,如果我的词是“FOO”? (including the question mark), I want to match: (包括问号),我想匹配:

"FOO? is cool", "do you think that FOO??" “FOO?很酷”,“你觉得那个FOO??”

but not: "FOO is cool", "FOO?is cool", "aaFOO?is cool"但不是:“FOO 很酷”、“FOO?很酷”、“aaFOO?很酷”

It should also work for words that start with "?".它也应该适用于以“?”开头的单词。 For instance, if my word if "?FOO", I want to match:例如,如果我的话如果“?FOO”,我想匹配:

"?FOO is cool", "I love ?FOO" “?FOO 很酷”、“我爱?FOO”

but not: "FOO is cool", "FOO?is cool", "aaFOO?is cool"但不是:“FOO 很酷”、“FOO?很酷”、“aaFOO?很酷”

I hope it makes sense.我希望这是有道理的。

The \\b word boundary construct is ambiguous. \\b字边界结构是模棱两可的。 You need to use unambiguous constructs that will make sure there are non-word chars or start/end of string to the left/right of the word matched.您需要使用明确的结构来确保匹配的单词的左/右有非单词字符或字符串的开始/结尾。

You may use您可以使用

/(?:^|\W)\?FOO\?(?!\w)/g

Here, (?:^|\\W) is a non-capturing group that matches either the start of a string or any non-word char, a char other than an ASCII letter, digit and _ .在这里, (?:^|\\W)是一个非捕获组,它匹配字符串的开头或任何非单词字符、ASCII 字母、数字和_以外的字符。 (?!\\w) is a negative lookahead that fails the match if, immediately to the right of the current location, there is a word char. (?!\\w)是一个否定的前瞻,如果在当前位置的右侧有一个单词 char,则匹配失败。

Or, with ECMAScript 2018 compatible JS environments,或者,使用 ECMAScript 2018 兼容的 JS 环境,

/(?<!\w)\?FOO\?(?!\w)/g

See this regex demo .请参阅此正则表达式演示

The (?<!\\w) is a negative lookbehind that fails the match if there is a word char immediately to the left of the current location. (?<!\\w)是一个否定的lookbehind,如果在当前位置的左侧有一个单词 char,则匹配失败。

In code, you may use it directly with String#match to extract all occurrences, like s.match(/(?<!\\w)\\?FOO\\?(?!\\w)/g) .在代码中,您可以直接将它与String#match一起使用来提取所有出现的事件,例如s.match(/(?<!\\w)\\?FOO\\?(?!\\w)/g)

The first expression needs a capturing group around the word you need to extract:第一个表达式需要一个围绕您需要提取的单词的捕获组:

 var strs = ["?FOO is cool", "I love ?FOO", "FOO is cool", "FOO?is cool", "aaFOO?is cool"]; var rx = /(?:^|\\W)(\\?FOO)(?!\\w)/g; for (var s of strs) { var res = [], m; while (m=rx.exec(s)) { res.push(m[1]); } console.log(s, "=>", res); }

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

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