I need a regular expression that will get digits only if theres no letters infront.
DoThis(0.5) // = 0.5 should be selected
DoThis5(43) // = 43 should be selected, but not 5
I managed to write this /[0-9.]+\b/g but it will select the ‘5’ from DoThis5(). Which is what I dont want.
If you have the
\boperator available, why don’t you do\b[0-9.]+(the plus is greedy, so will eat all the digits it finds).Edit: With a stricter regex for decimal numbers with zero or one dot and at least one number;
This currently disallows 10. and .5. It requires a regex engine which supports parentheses for grouping, and question mark for an optional part. (Some old versions of
sedand e.g. Emacs require you to backslash the parentheses and the question mark.)