简体   繁体   中英

Regex match word after negated set

I'm currently trying to match the following cases with 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

1. \\.\\/ This will match ./

2. (atoms|molecules|organisms) This will match either atoms or molecules or organisms

3. \\/[^\\/]+\\/ This will match / and then till /

4. index\\.js This will match 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 . 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. az accounts for lower case while AZ accounts for upper case. * 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]* .
  3. \\/index\\.js will match '/index.js'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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