匹配以特殊字符开头或结尾的整个单词 [英] Matching whole words that start or end with special characters

查看:65
本文介绍了匹配以特殊字符开头或结尾的整个单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要JavaScript中的正则表达式来匹配以特殊字符开头或结尾的整个单词吗?

I need a regular expression in javascript that matches whole words that start or end with special characters?

这本来应该很容易,但是由于某些原因,?之后的\b并没有达到我的预期:

It was supposed to be easy, but for some reason \b after ? doesn't behave as I expected:

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

我需要什么,例如,如果我的单词是"FOO"? (包括问号),我要匹配:

What I need, for instance if my word is "FOO?" (including the question mark), I want to match:

"FOO?很酷",您认为FOO吗?"

"FOO? is cool", "do you think that FOO??"

但不是:"FOO很酷","FOO?很酷","aaFOO?很酷"

but not: "FOO is cool", "FOO?is cool", "aaFOO?is cool"

它也适用于以?"开头的单词.例如,如果我的单词为?FOO",我想匹配:

It should also work for words that start with "?". For instance, if my word if "?FOO", I want to match:

?FOO很酷",我爱?FOO"

"?FOO is cool", "I love ?FOO"

但不是:"FOO很酷","FOO?很酷","aaFOO?很酷"

but not: "FOO is cool", "FOO?is cool", "aaFOO?is cool"

我希望这是有道理的.

推荐答案

\b单词边界构造是不明确的.您需要使用明确的结构,以确保匹配的单词的左/右有非单词字符或字符串的开始/结尾.

The \b word boundary construct is ambiguous. 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.

您可以使用

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

在这里,(?:^|\W)是一个非捕获组,它与字符串的开头或任何非单词char,ASCII字母,数字和_以外的char匹配. (?!\w)是否定的超前查询,如果在当前位置的右边立即有一个单词char,则匹配失败.

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) is a negative lookahead that fails the match if, immediately to the right of the current location, there is a word char.

或者,在与ECMAScript 2018兼容的JS环境中,

Or, with ECMAScript 2018 compatible JS environments,

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

请参见此regex演示.

(?<!\w)是一个向后查找的否定字符,如果当前位置左侧紧接着有一个字符char,则匹配失败.

The (?<!\w) is a negative lookbehind that fails the match if there is a word char immediately to the left of the current location.

在代码中,您可以将其直接与String#match一起使用以提取所有出现的内容,例如s.match(/(?<!\w)\?FOO\?(?!\w)/g).

In code, you may use it directly with String#match to extract all occurrences, like 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);
}

这篇关于匹配以特殊字符开头或结尾的整个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆