I need to know if there is a regular expression for testing for the presence of numbers in strings that:
- Matches
Lorem 20 Ipsum - Matches
Lorem 2,5 Ipsum - Matches
Lorem 20.5 Ipsum - Does not match
Lorem 2% Ipsum - Does not match
Lorem 20.5% Ipsum - Does not match
Lorem 20,5% Ipsum - Does not match
Lorem 2 percent Ipsum - Does not match
Lorem 20.5 percent Ipsum - Does not match
Lorem 20,5 percent Ipsum - Matches
Lorem 20 Ipsum 2% dolor - Matches
Lorem 2,5 Ipsum 20.5% dolor - Matches
Lorem 20.5 Ipsum 20,5% dolor
That is, a regular expression that can tell me if in a string there is one or many numbers, but not as percentage value.
I’ve tried something as /[0-9\.,]+[^%]/, but this not seems to work, I think because digits then not a percentage sign match also the 20 in the string 20%. Additionally, I don’t know how to tell not the entire percent string in addition to the % char.
This will do what you need:
–EDIT–
The meat here is the use of a “negative lookahead” on the final line that causes the match to fail if any of a percent-sign or the literal “percent” occurs after a number and one or more spaces. Other uses of negative lookahead in JavaScript RegExps can be found at Negative lookahead Regular Expression
–2ND EDIT–
Congrats to Enrico for solving the most general case but while his solution below is correct, it contains several extraneous operators. Here is the most succinct solution.