Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript RegExp不包括特定的文件扩展名和index.js_Javascript_Regex_Regex Lookarounds - Fatal编程技术网

JavaScript RegExp不包括特定的文件扩展名和index.js

JavaScript RegExp不包括特定的文件扩展名和index.js,javascript,regex,regex-lookarounds,Javascript,Regex,Regex Lookarounds,这是我当前的RegExp: /^(?!index).*js/gmi 这是应用RegExp的列表: test/test.js hey/test-bundle.js test/test-heybundle.js test/index.js test/indop.js lollipop/testindex.js test/test.scss test/test.css lalilu/hey.yml test.js 它应该做什么: 仅匹配以*.js结尾的文件 排除文件名index.js 排除以“-

这是我当前的RegExp:

/^(?!index).*js/gmi
这是应用RegExp的列表:

test/test.js
hey/test-bundle.js
test/test-heybundle.js
test/index.js
test/indop.js
lollipop/testindex.js
test/test.scss
test/test.css
lalilu/hey.yml
test.js
它应该做什么:

  • 仅匹配以*.js结尾的文件
  • 排除文件名index.js
  • 排除以“-bundle.js”结尾的文件
  • 仅匹配位于目录中的文件(例如
    /test/test.js
    ,而不是
    test.js
我真的非常感谢任何能让这个RegExp按预期工作的帮助。

/^[a-z]+\/(?!index\.js$)[a-z]+(?!-bundle)(?:-[a-z]+)?\.js$/gm

^[a-z]+\/
-匹配父目录(假设目录分隔符始终为
/

(?!index\.js$)
-如果父目录后跟
index.js
和行尾,则匹配失败

[a-z]+(?!-bundle)
-一个或多个字母后面不跟
-bundle

(?:-[a-z]+)?
-可选文件名的第二部分,后跟连字符。如果您还有包含两个以上连字符部分的文件,请将
更改为
*


\.js$
-文件扩展名和行尾。

无法使捆绑包部分匹配。Far is get was
^.*\/(?!index)。*\.js
-这解决了除了捆绑包之外的所有问题。谢谢!这对其他人来说是一个很好的起点。在您的用例中不可能运行多个正则表达式吗?
^(?=[^.]+\.js$)(?=[^/]+\/)(?!.*(:index |-bundle)\.js)。*$
我刚刚注意到了“bug”,即使使用
*
处理包含2个以上破折号的文件名,如您所建议的:它不应与倒数第二行匹配,因此在匹配子目录中的文件时遇到问题: