输入是绝对路径,例如:
/usr/local/lib/node_modules/normalize
/usr/local/lib/node_modules/bobcat/index.js
我想创建一个匹配所有内容的正则表达式,但在字符串中找到“ bobcat”时除外:
这就是我要匹配的所有内容:
var pattern = /node_modules/g;
var matches = pattern.test(input);
我怎样才能做到这一点?
最佳答案
您可以使用否定的前瞻正则表达式:
/node_modules(?!.*\/bobcat\/)/g
RegEx Demo
(?!.*\/bobcat\/)
是负向超前行,如果/bobcat/
在node_modules
之后,则匹配失败。