[英]Regex match word after negated set
I'm currently trying to match the following cases with Regex. 我目前正在尝试将以下案例与Regex相匹配。
Current regex 目前的正则表达式
\\.\\/[^/]\\satoms\\s\\/[^/]+\\/index\\.js
Cases 案例
// Should match
./atoms/someComponent/index.js
./molecules/someComponent/index.js
./organisms/someComponent/index.js
// Should not match
./atomsdsd/someComponent/index.js
./atosdfms/someComponent/index.js
./atomssss/someComponent/index.js
However none of the cases are matching, what am I doing wrong? 然而,没有一个案例是匹配的,我做错了什么?
Hope this will help you out. 希望这会帮助你。 You have added some addition characters which lets your regex to fail.
你添加了一些额外的字符,让你的正则表达式失败。
Regex: \\.\\/(atoms|molecules|organisms)\\/[^\\/]+\\/index\\.js
正则表达式:
\\.\\/(atoms|molecules|organisms)\\/[^\\/]+\\/index\\.js
1.
\\.\\/
This will match./
1.
\\.\\/
这将匹配./
2.
(atoms|molecules|organisms)
This will match eitheratoms
ormolecules
ororganisms
2.
(atoms|molecules|organisms)
这将匹配atoms
或molecules
或organisms
3.
\\/[^\\/]+\\/
This will match/
and then till/
3.
\\/[^\\/]+\\/
这将匹配/
然后直到/
4.
index\\.js
This will matchindex.js
4.
index\\.js
这将匹配index.js
为什么不只是这个简单的模式 ?
\.\/(atoms|molecules|organisms)\/.*?index\.js
Try the following: 请尝试以下方法:
\.\/(atoms|molecules|organisms)\/[a-zA-Z]*\/index\.js
Forward slashes (and other special characters) should be escaped with a back slash \\
. 应使用反斜杠
\\
来转义正斜杠(和其他特殊字符)。
\\.\\/(atoms|molecules|organisms)\\/
matches '.atoms/' or .molecules
or organisms
strictly . \\.\\/(atoms|molecules|organisms)\\/
严格匹配'.atoms /'或.molecules
或organisms
。 Without the parenthesis it will match partial strings. |
|
is an alternation operator that matches either everything to the left or everything to the right. [a-zA-Z]*
will match a string of any length with characters in any case. [a-zA-Z]*
将匹配任何长度的字符串和字符。 az
accounts for lower case while AZ
accounts for upper case. az
占小写,而AZ
占大写。 *
indicates one or more characters. *
表示一个或多个字符。 Depending on what characters may be in someCompenent
you may need to account for numbers using [a-zA-Z\\d]*
. someCompenent
你可能需要考虑使用数字[a-zA-Z\\d]*
。 \\/index\\.js
will match '/index.js' \\/index\\.js
将匹配'/index.js'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.