简体   繁体   English

否定集后正则表达式匹配单词

[英]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 either atoms or molecules or organisms 2. (atoms|molecules|organisms)这将匹配atomsmoleculesorganisms

3. \\/[^\\/]+\\/ This will match / and then till / 3. \\/[^\\/]+\\/这将匹配/然后直到/

4. index\\.js This will match index.js 4. index\\.js这将匹配index.js

Regex demo 正则表达式演示

为什么不只是这个简单的模式

\.\/(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 \\ . 应使用反斜杠\\来转义正斜杠(和其他特殊字符)。

  1. \\.\\/(atoms|molecules|organisms)\\/ matches '.atoms/' or .molecules or organisms strictly . \\.\\/(atoms|molecules|organisms)\\/ 严格匹配'.atoms /'或.moleculesorganisms Without the parenthesis it will match partial strings. 如果没有括号,它将匹配部分字符串。 the | | is an alternation operator that matches either everything to the left or everything to the right. 是一个交替运算符,它匹配左侧的所有内容或右侧的所有内容。
  2. [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]*
  3. \\/index\\.js will match '/index.js' \\/index\\.js将匹配'/index.js'

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

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