I'm looking for a regular expression pattern, that matches everything but one exact word.
For example, resolutions:
monitors/resolutions // Should not match
monitors/34 // Should match
monitors/foobar // Should match
I know that you can exclude a list of single characters, but how do you exclude a complete word?
Use negative lookahead assertion,
^(?!.*resolutions).*$
OR
^(?!.*\bresolutions\b).*$
DEMO
function test(str){
let match,
arr = [],
myRe = /monitors\/((?:(?!resolutions)[\s\S])+)/g;
while ((match = myRe.exec(str)) != null) {
arr.push(match[1]);
}
return arr;
}
console.log(test('monitors/resolutions'));
console.log(test('monitors/34'));
console.log(test('monitors/foobar'));
function test(str){
let match,
arr = [],
myRe = /monitors\/(\b(?!resolutions\b).+)/g;
while ((match = myRe.exec(str)) != null) {
arr.push(match[1]);
}
return arr;
}
console.log(test('monitors/resolutions'));
console.log(test('monitors/34'));
console.log(test('monitors/foobar'));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With