正则表达式为子串而不是子串

Lik*_*d_T -2 javascript regex

我需要在Javascript中使用RegEx模式,该模式将包含.html以及以字符串结尾的所有文件名rgo1.

我还需要Javascript中的RegEx模式,它将排除所有.html以及包含字符串的文件名rgo1.

这是webpack.config中的文件名匹配,这就是我想要这个模式的原因.

谢谢你的"包含"模式,我找到了一个有效的模式,我现在会在我的"不包含模式"中粘贴失败的尝试:

[^rgo1].*\.html$/gm // nope
(?!^rgo1$).*\.html$ // nope
^((?!rgo1).)*$.*\.html$ // nope
^((?!rgo1).)*.\.html$ //works...paste this in your answer for the win
Run Code Online (Sandbox Code Playgroud)

Rah*_*sai 5

使用正则表达式: /rgo1.*\.html$/

正则表达式说明:它基本上匹配一个包含rgo1后跟任何内容并以.html.结尾的字符串.

对于问题的其他部分,只是否定了 .test()

演示:

var filename1 = 'xyzrgo1abc.html';
var filename2 = 'rgo1abc.html';

var regex = new RegExp('rgo1.*\\.html$');

console.log('filename1 test:', regex.test(filename1));
console.log('filename2 test:', regex.test(filename1));
console.log('filename1 negation test:', !regex.test(filename1));
Run Code Online (Sandbox Code Playgroud)

注意:对于非正则表达式解决方案,请参阅@revo的答案