如何在结果中不包含定界符的情况下匹配所有出现的单词
How to match all occurrences of a word without including delimiters in results
我有一个输入字符串s="blah, blah.blah blah foöblah blah"
考虑将 whitespace
、,
和 .
作为分隔符,我想匹配 blah 的所有完整单词出现,例如用 x
替换所需的匹配项后,上面的字符串应该类似于 x, x.x x foöblah x
我试过遵循正则表达式
/(?:\s|.|,|^)blah(?=\s|.|,|$)/g
但它有以下问题
- 它与
foöblah
中的 blah
匹配
- 它包括与周围定界符的匹配,这是不需要的。 我只想要废话
EDIT#1:输入字符串 s
可以包含重音字符,因此使用单词边界作为正则表达式将不起作用。
Matches a word boundary. A word boundary matches the position where a word character is not followed or preceeded by another word-character. Note that a matched word boundary is not included in the match. In other words, the length of a matched word boundary is zero. (Not to be confused with [\b].)
Examples:
/\bm/ matches the 'm' in "moon" ;
/oo\b/ does not match the 'oo' in "moon", because 'oo' is followed by 'n' which is a word character;
/oon\b/ matches the 'oon' in "moon", because 'oon' is the end of the string, thus not followed by a word character;
/\w\b\w/ will never match anything, because a word character can never be followed by both a non-word and a word character.(Taken from here)
s = "blah, blah.blah blah fooblah blah";
console.log(
s.replace(/\bblah\b/g, 'x')
)
更新:
使用您自己的正则表达式,您需要通过 \.
转义 .
,同时使用 </code> 将添加捕获的组值替换为字符串
<strong><a href="https://regex101.com/r/gX3sT1/1" rel="nofollow">Regex explanation here</a></strong></p>
<p><div class="snippet" data-lang="js" data-hide="false" data-console="true">
<div class="snippet-code">
<pre><code>s = "blah, blah.blah blah fooblah blah";
console.log(
s.replace(/(\s|\.|,|^)blah(?=\s|\.|,|$)/g, 'x')
)
我有一个输入字符串s="blah, blah.blah blah foöblah blah"
考虑将 whitespace
、,
和 .
作为分隔符,我想匹配 blah 的所有完整单词出现,例如用 x
替换所需的匹配项后,上面的字符串应该类似于 x, x.x x foöblah x
我试过遵循正则表达式
/(?:\s|.|,|^)blah(?=\s|.|,|$)/g
但它有以下问题
- 它与
foöblah
中的 - 它包括与周围定界符的匹配,这是不需要的。 我只想要废话
blah
匹配
EDIT#1:输入字符串 s
可以包含重音字符,因此使用单词边界作为正则表达式将不起作用。
Matches a word boundary. A word boundary matches the position where a word character is not followed or preceeded by another word-character. Note that a matched word boundary is not included in the match. In other words, the length of a matched word boundary is zero. (Not to be confused with [\b].)
Examples: /\bm/ matches the 'm' in "moon" ; /oo\b/ does not match the 'oo' in "moon", because 'oo' is followed by 'n' which is a word character; /oon\b/ matches the 'oon' in "moon", because 'oon' is the end of the string, thus not followed by a word character; /\w\b\w/ will never match anything, because a word character can never be followed by both a non-word and a word character.(Taken from here)
s = "blah, blah.blah blah fooblah blah";
console.log(
s.replace(/\bblah\b/g, 'x')
)
更新:
使用您自己的正则表达式,您需要通过 \.
转义 .
,同时使用 </code> 将添加捕获的组值替换为字符串
<strong><a href="https://regex101.com/r/gX3sT1/1" rel="nofollow">Regex explanation here</a></strong></p>
<p><div class="snippet" data-lang="js" data-hide="false" data-console="true">
<div class="snippet-code">
<pre><code>s = "blah, blah.blah blah fooblah blah";
console.log(
s.replace(/(\s|\.|,|^)blah(?=\s|\.|,|$)/g, 'x')
)