Jump to content

Simple RegExp Issue


MrFish

Recommended Posts

I'm trying to parse a string to see if it's a valid real number. The first issue I've run into is with java the \w flag returns a digit. A work around I'm using for integers is just \D which does work but now I've got the check for digits and a single decimal point (if exists)

// Trying to parse if string is an interger or real number// Should find any non-numeric charactersvar invalidCharacters = "[\\w]";var reg = new RegExp(invalidCharacters);reg.exec(400); // Finds "4"

Could anyone shed light on this?

Link to comment
Share on other sites

I'm trying to parse a string to see if it's a valid real number...
// Trying to parse if string is an interger or real number// Should find any non-numeric characters

Do you want to find all of the invalid characters (and store them for example) or do you just want to validate if the string only contains numbers (returns true/false)? If you only want to validate, this regex would be fine:
^[\d,]+$

It will also say that a string like this: "589,5523,523" is valid. If this is not what you are looking for, could you explain a little more :)

Link to comment
Share on other sites

I didn't want any commas in the number.

You can use this to validate a real number: ^\d+(\.\d+)?$ That should be 1 or more digits, followed optionally by a decimal point then 1 or more digits.
This should work. Thanks
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...