Tuesday, 10 September 2013

Regular Expression To Fail On Character In One Place But Match If Character Used Later On

Regular Expression To Fail On Character In One Place But Match If
Character Used Later On

I have a regular expression (Javascript) that can take whitespace followed
by an optional sign [-+] followed by number characters [0-9.,] followed by
more optional whitespace. What I want to do is make sure the next
character is not +,-,*,/ or ^. The next character can be any other text
and, once that other text is entered, can then include any math symbols.
Here's what I have so far:
^\s*[-+]?[0-9,.]+\s*[^-+*/^].*$
This allows both "23 +" and "23 df+" to match, though, and I only want the
latter example to match. If I leave off the .* near the end then "23 +"
and "23 df+" both fail. I thought the bracket expression [^-+*/^] for the
math characters would match the next character and then .* would match
characters after that, but that doesn't appear to be the case.
Can anyone help explain both how to fix it and where I went wrong? I'm new
to regex and would like to learn from my mistake.

No comments:

Post a Comment