[英]Javascript regexp match with exception
The input is an absolute path for example: 输入是绝对路径,例如:
/usr/local/lib/node_modules/normalize
/usr/local/lib/node_modules/bobcat/index.js
I want to create a regex that matches EVERYTHING except when "bobcat" was found in the string: 我想创建一个匹配所有内容的正则表达式,但在字符串中找到“ bobcat”时除外:
This is what I got to match everything: 这就是我要匹配的所有内容:
var pattern = /node_modules/g;
var matches = pattern.test(input);
How can I do this? 我怎样才能做到这一点?
You can use a negative lookahead regex: 您可以使用否定的前瞻正则表达式:
/node_modules(?!.*\/bobcat\/)/g
(?!.*\\/bobcat\\/)
is negative lookahead that will fail the match if /bobcat/
comes after node_modules
. (?!.*\\/bobcat\\/)
为负先行,如果/bobcat/
在node_modules
之后,则匹配失败。
Make an if/else statement and check if /bobycat/ matches. 做出if / else语句,并检查/ bobycat /是否匹配。 Put your code in the else part.
将您的代码放在其他部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.