How to write an inverse regex that will match when a string is not present

(?=^((?!the text that should not be present).)+$)(^the text that should be present$)

Here is a Javascript snippet to check that a string does not contain two dashes (–) directly following each other:

var regex = /(?=^((?!--).)+$)(^[a-z\-]{1,10}$)/;
true === regex.test('abc-def');
true === regex.test('-abc-def-');
false === regex.test('abc--def');
false === regex.test('abc---def');

You may also like...

Popular Posts