Question

^(!?\/[a-zA-Z0-9-_]*\/[a-zA-Z0-9-_]*\/[a-zA-Z0-9]{13})$

This the regex, I want to match every URL except the ones that have /something/something/13-character-group

So if I make:

/something/something/123456789012 this would match

/something/something/1234567890123 this would not match

Tnx alot!

Was it helpful?

Solution

By matching every URL except those having /something/something/13-character-group, I'm assuming that your input strings will have characters before and after those you mentioned in your question.

First point to note: a negative lookahead is invoked with (?! ... ) (and not !? because this means 1 ! character or none). Then, to look for this string anywhere in the URL, you will have to use a .* before the pattern and a word boundary after the pattern:

^(?!.*/[a-zA-Z0-9-_]*/[a-zA-Z0-9-_]*/[a-zA-Z0-9]{13}\b).+$

The last .+ is to match the actual URL.

OTHER TIPS

This should work!

var str1 = '/something/something/123456789012';
var str2 = '/something/something/1234567890123';

var re = /^(\/[a-zA-Z0-9-_]+\/[a-zA-Z0-9-_]+\/(?![a-zA-Z0-9]{13}$)[a-zA-Z0-9]+)$/;

alert(re.test(str1));
alert(re.test(str2));

It looks like this.

regex

Image from Regexper.com

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top