4

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!

Robin Mackenzie
  • 14,370
  • 7
  • 36
  • 44
Edy Bourne
  • 4,229
  • 9
  • 42
  • 75

4 Answers4

10

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."

Community
  • 1
  • 1
Cirno
  • 116
  • 3
  • Awesome! I knew I was close, but couldn't figure out what was missing... the .*$ did the trick!! Thank you!! – Edy Bourne Apr 19 '13 at 05:47
2

Instead of thinking about a negative regular expression, make a positive one and negate the condition:

if (! /^Select an account$/.test(mystring)) {
  // String is not "Select an account"
}

But you don't need regex for this anyway:

if (mystring != 'Select an account') {
  // String is not "Select an account"
}

Or if you want "contains":

if (mystring.indexOf('Select an account') == -1) {
  // String does not contain "Select an account"
}
elclanrs
  • 85,039
  • 19
  • 126
  • 159
  • Oh, like I said I cannot use that. I will feed the regex pattern to a form validation framework, so I can't negate the test or do the obvious thing. I won't actually be executing the test... just need a simple regex that will match a given string and nothing else. There's gotta be a way to do that... – Edy Bourne Apr 19 '13 at 04:45
0

I may not be understanding this correctly. Wouldn't this work?

if (string != "Select a value") {
    //code here
}

I don't know why you would need regular expressions to do that, which is why I'm afraid I might not be understanding what your question is.

vijrox
  • 981
  • 1
  • 11
  • 33
  • Oh, thanks Vijay, I got the answer above, but the reason I couldn't do that was because I will feed the regex pattern to a form validation framework which takes a regex as input. I won't be making the check myself didn't want to temper with the framework's code. – Edy Bourne Apr 19 '13 at 05:49
0

An additional solution, if you’re dealing with regex in HTML5 form validation and the pattern attribute of your input is more complex than one exact string, is as follows (javascript):

function findAllNotPattern(inputField) {
    var allBut1 = inputField.getAttribute("pattern")
    var allBut2 = allBut1.slice(0, 1) + "^" + allBut1.slice(1)
    var allButRe = new RegExp(allBut2, "g")
    return allButRe
}

if you want to run it in pure javascript, you'd use:

findAllNotPattern(document.getElementById("inputFieldId"))
E.S.
  • 1