BTDevPro
BTDevPro

Reputation: 13

RegExp: Ignore 2 consecutive slashes but match 1 single slash

I'm creating a string validator using RegExp and I need it to match single slash (/) but not match double slashes (// OR https?://). I need exactly the opposite of what (\/{2,2}) does.

The final RegExp I need will be consisted from this sub expression and [^A-Za-zÀ-ü0-9\.:_ @ü+-]

Thanks for help!

Upvotes: 1

Views: 807

Answers (1)

logi-kal
logi-kal

Reputation: 7880

If you want to match also the three slashes you can use this:

(?:^|[^\/])(\/(?:(?!\/)|\/{2,}))

For the rest, ou are right, there is no AND in regular expressions, but there is OR :-) For joining the two regexps you can use this:

^(?:[^A-Za-zÀ-ü0-9.:_ @ü+\-\/]|\/(?:(?!\/)|\/{2,}))*$

Notice that I added a \/ in the first member of the alternation. So you remove from the valid characters also the slash, but you re-add it using alternation ensuring at the same time that it is not followed by a second slash OR it is followed by at least two slashes.

See a demo here.

Upvotes: 1

Related Questions