Question

I have a regular expression for matching price where decimals are optional like so,

/[0-9]+(\.[0-9]{1,2})?/

Now what I would like to do is get the inverse of the expression, but having trouble doing so. I came up with something simple like,

/[^0-9.]/g

But this allows for multiple '.' characters and more than 2 numbers after the decimal. I am using jQuery replace function on blur to correct an input price field. So if a user types in something like,

"S$4sd3.24151 . x45 blah blah text blah" or "!#%!$43.24.234asdf blah blah text blah"

it will return

43.24

Can anyone offer any suggestions for doing this?

Was it helpful?

Solution

I would do it in two steps. First delete any non-digit and non-dot-character with nothing.

/[^0-9.]//g

This will yield 43.24151.45 and 43.24.234 for the first and second example respectively. Then you can use your first regex to match the first occurence of a valid price.

/\d(\.\d{1,2})?/

Doing this will give you 43.24 for both examples.

OTHER TIPS

I suppose in programming, it is not always clear what "inverse" means.

To suggest a solution exclusively based on the example that you presented, I will present one that is very similar to what Vince presented. I am having difficulty composing a Regular Expression that both matches the pattern that you need and captures a potentially arbitrary number of digits, through repeating capture groups. And I am not sure whether this would be doable in some reasonable way (perhaps someone else does). But a two step approach should be straightforward.

To note, I suspect that you are referring to JavaScript's replace function, which is a member of the String Object, and not jQuery replaceWith and replaceAll functions, in referring to 'jQuery replace function.' The latter are 'Dom manipulation' functions. But, correct me if I misunderstood.

As an example, based on some hypothetical input, you could use

<b>var numeric_raw = jQuery('input.textbox').attr ('value').replace (/[^0-9.]/g, "")</b>

to remove all characters from a value entered in a text field that are not digits or periods; then you could use

<b>var numeric_str = numeric_raw.replace (/^[0]*(\d+\.\d{1,2}).*$/, "$1")</b>

The difference between the classes specified here and in Vince's answer are in that I am including filtering for leading 0s. To note, in Vince's first reg ex, there might be an extra '/' -- but perhaps it has a purpose that I didn't catch.

With respect to "inverse," one way to understand your initial inquiry is that you are looking for an expression that does the opposite of the one that you provided. To note, while the expression that you provided (/[0-9]+(.[0-9]{1,2})?/) does match both whole numbers and decimal numbers with up to two fractional digits, it also matches any single digit -- so, it may identify a match where one might not be envisioned, for a given input string. The expression does not have anchors ('^', '$'), and so might allow multiple possible matches. For example, in the String "1.111", both "1.11" and "1" match the pattern that you provided.

It appears to me that one pattern that matches any string that does not match your pattern is the following, or at least does this for most cases can be this:

/^(?:(?!.*[0-9]+(\.[0-9]{1,2})?).*)*$/

-- if someone could identify a precisely 'inverse' pattern, please feel free -- I am having some trouble understanding how lookaheads are interpreted at least for some nuances.

This relies on "negative lookahead" functionality, which JavaScript these days supports. You could refer to several stackoverflow postings for more information (eg. Regular Expressions and negating a whole character group), and there are multiple resources that could be found on the Internet that discuss "lookahead" and "lookbehind."

I suppose this answer carries some redundancy with respect to the one already given -- I might have commented on the Original Poster's post or on Vince's answer (instead of writing at least parts of my answer), but I am not yet able to make comments!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top