I am new to regular expression and this may be a very easy question (hopefully).
I am trying to use one solution for 3 kinds of string
- "45%", expected result: "45"
- "45", expected result: "45"
- "", expected result: ""
What I am trying (let the string be str):
str.match(/(.*)(?!%*)/i)[1]
This is in my head would sound like "match any instance of anything up until '%' if it is found, or else just match anything"
In firebug's head, it seems to sound more like "just match anything and completely disregard the negative lookahead". Also to make it lazy - (.*)?
- doesn't seem to help.
Let's forget for a second that in this specific situation I am only matching numbers, so a /\d*/
would do. I am trying to understand a general rule so that I can apply it whenever.
Anybody would be so kind to help me out?