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');

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s