[英]RegEx: Match a word with exactly one occurrence of a specified character at the beginning
I want to detect words starting with $
but ignore words starting with $$
because I want to give the user a way to escape that character. 我想检测以
$
开头的单词,但是忽略以$$
开头的单词,因为我想为用户提供一种逃脱该字符的方式。
I have tried many things, but the nearer I got was this: [^\\$]\\$\\w+
我已经尝试了很多东西,但是更接近的是:
[^\\$]\\$\\w+
It matches occurrences like The side bar $$includes a| $Cheatsheet|, full
它与出现的情况相匹配,例如
The side bar $$includes a| $Cheatsheet|, full
The side bar $$includes a| $Cheatsheet|, full
with the white space at the beginning of the word $Cheatsheet
included. The side bar $$includes a| $Cheatsheet|, full
包括在单词$Cheatsheet
开头的空白。 It should match the word $Cheatsheet
only, without the space. 它应该只与单词
$Cheatsheet
匹配,没有空格。
How can I do it? 我该怎么做? Any ideas?
有任何想法吗?
The regex you tried [^\\$]\\$\\w+
will match not a dollar sign followed by a dollar sign and one or more times a word character. 您尝试过的正则表达式
[^\\$]\\$\\w+
将不匹配美元符号,后跟美元符号以及一个或多个单词字符。 That would match for example a$Cheatsheet
or $Cheatsheet
with a leading space. 例如,这将匹配
a$Cheatsheet
或带有前导空格的$Cheatsheet
。 Note that you don't have to escape the dollar sign in the character class. 注意,您不必在字符类中转义美元符号。
If negative lookbehinds are supported, to match a word that does not start with a dollar sign you could use: 如果支持负向后搜索 ,则可以使用以下字符匹配不以美元符号开头的单词:
(?<!\\$)\\$\\w+
Without a lookbehind you could match what you don't want and capture what you do want in a capturing group. 没有后顾之忧,您可以匹配不需要的内容,并在捕获组中捕获所需的内容。
\\${2}\\w+|(\\$\\w+)
If the dollar sign can also not be in the middle of the word you could use: 如果美元符号也不能位于单词的中间,则可以使用:
\\S(?:\\$+\\w+)+\\$?|(\\$\\w+)
Since negative lookbehinds are NOT yet supported in all JavaScript engines ( https://github.com/tc39/proposal-regexp-lookbehind ), you can start with your regex and introduce a match group: 由于所有JavaScript引擎( https://github.com/tc39/proposal-regexp-lookbehind )尚不支持否定的lookbehinds,因此您可以从regex开始并引入一个match组:
[^\$](\$\w+)
then, to exclude aaa$bbb
, it is possible to use: 然后,要排除
aaa$bbb
,可以使用:
\s(\$\w+)
edit : and to match at the beginning or after punctuation: 编辑 :,并在标点符号的开头或之后进行匹配:
(?:^|[^$\w])(\$\w+)
https://regex101.com/r/3cW5oY/2 https://regex101.com/r/3cW5oY/2
You want to escape a $
with $
. 你想逃避
$
有$
。 That means, you need 这意味着,您需要
/(?:^|[^$])(?:\${2})*\B(\$\w+)/g
See the regex demo . 参见regex演示 。
Details 细节
(?:^|[^$])
- start of string or any char but $
(?:^|[^$])
-字符串的开头或除$
任何字符 (?:\\${2})*
- 0 or more repetitions of double dollar (this is required to avoid matching literal dollars) (?:\\${2})*
-重复两次或多次重复两次(避免与文字美元匹配) \\B
- requires start of string or non-word char before the next $
\\B
在下一个$
之前需要字符串或非单词char开头 (\\$\\w+)
- Group 1: a $
and then 1+ word chars. (\\$\\w+)
-第1组:一个$
,再加上1个以上的字符字符。 JS demo: JS演示:
var s = "The $side bar $$includes a| $Cheatsheet|, $$$$full aaa$side"; var res = [], m; var rx = /(?:^|[^$])(?:\\${2})*\\B(\\$\\w+)/g; while(m=rx.exec(s)) { res.push(m[1]); } console.log(res);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.