Javascript 否定集后的正则表达式匹配字

Javascript 否定集后的正则表达式匹配字,javascript,regex,Javascript,Regex,我目前正在尝试将以下案例与Regex匹配 当前正则表达式 \.\/[^/]\satoms\s\/[^/]+\/index\.js 案例 // Should match ./atoms/someComponent/index.js ./molecules/someComponent/index.js ./organisms/someComponent/index.js // Should not match ./atomsdsd/someComponent/index.js ./atosdfm

我目前正在尝试将以下案例与Regex匹配

当前正则表达式

\.\/[^/]\satoms\s\/[^/]+\/index\.js

案例

// 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

但是没有一个案例是匹配的,我做错了什么?

希望这能帮助您解决问题。您添加了一些附加字符,使正则表达式失败

Regex:
\.\/(原子、分子、生物体)\/[^\/]+\/index\.js

1。
\.\/
这将匹配
/

2.
(原子|分子|生物体)
这将匹配
原子
分子
生物体

3.
\/[^\/]+\/
这将匹配
/
,然后直到
/

4.
index\.js
这将匹配
index.js

尝试以下操作:

\.\/(atoms|molecules|organisms)\/[a-zA-Z]*\/index\.js
正斜杠(和其他特殊字符)应使用反斜杠转义
\

  • \.\/(原子分子生物体)\/
    严格匹配“.atoms/”或
    。分子
    生物体
    。如果没有括号,它将匹配部分字符串。
    |
    是一个交替运算符,它匹配左侧或右侧的所有内容
  • [a-zA-Z]*
    在任何情况下都会将任何长度的字符串与字符匹配
    a-z
    用于小写,而
    a-z
    用于大写<代码>*表示一个或多个字符。根据
    somecompent
    中可能包含的字符,您可能需要使用
    [a-zA-Z\d]*
    说明数字
  • \/index\.js
    将匹配'/index.js'
  • 为什么不仅仅是这个


    @anubhava在javascript中使用它,通过正则表达式中的
    \s
    标记进行测试时说,中的斜杠和文本字符串“atom”之间必须有空格字符,根据您的示例,这不是您想要的。
    someComponent/index.js
    ,我的好极了!请参阅我的更新问题,忘记在同一行中再添加两个我希望匹配的单词(如果可能)。非常感谢你!我会尽快接受,很好的答案现在我终于知道怎么做了。我是正则表达式中的一个不速之客——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
    \.\/(atoms|molecules|organisms)\/.*?index\.js