Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What would be the JavaScript pattern to match anything except a given string? [duplicate]

I need to make sure a given string is not "Select a value". That's it, anything else should match the pattern, except this exact string.

I've been looking around and trying many combinations on http://www.regular-expressions.info/javascriptexample.html but nothing seems to do the trick.

I can't negate the test, the pattern needs to do it all since I'll feed this to a form validation framework. If the select contains this text, I'll return an error, etc. So the pattern must match anything else, except this exact string,

I tried lots of things,

(?!Select an account)
!(Select an account)
!(^(Select an account)$)

etc... clearly I don't understand how some of these mechanisms work. I get the "starts with" and 'ends with", but I don't seem to find a simple negation operator.

For some reason everywhere I look for regex explanations I don't get this simple use case, maybe it's not common.

How can I accomplish this?

Thank you!

like image 614
Edy Bourne Avatar asked Dec 15 '22 11:12

Edy Bourne


1 Answers

I believe this was asking something similar:

Regular Expressions and negating a whole character group

In your case you could use something like

    ^(?!Select a value).*$

Which would match everything that does NOT start with "Select a value."

like image 168
Cirno Avatar answered Dec 28 '22 23:12

Cirno