0

i retrive a text from a div with the following options : "1,000" $ OR "1,000.57" $ "20" OR "20.5"

i need to extract the number only ( int \ float) without the comma sign and without the dollar sign. as for now im using the following mask : /\d+/g;

but with this mask i get the number when it doesnt include comma and it get the number only to the decimal point ( e.g 290.5 -> 290 instead of it's original ) i'm looking for the correct regex mask for that so i will be able to parse the number after i get it .

3
  • Idk why your question is downvoted. this seems perfectly legit to me. Commented Jul 27, 2015 at 17:34
  • so in short you want just the numbers with the decimals regardless how long but without letters (currency symbols) and without a comma separating the 1000 mark of the values? e.g "1,000" $ and "1,000.45867" $ to be 1000 and 1000.45867 respective? - will this div only take one value or will it take multiple values separated by something? Commented Jul 27, 2015 at 17:37
  • Ah, perhaps I misread the question. I thought you were trying to parse out that entire string, including quotes and ORs from a div. Commented Jul 27, 2015 at 17:40

2 Answers 2

1

You could do it like this:

var str = ... // gets the text from the div
var num = Number(str.replace(/\$|,/g, '')); // converts the string to a number
                                            // (either a float or an int, depending
                                            // on the input string)
Sign up to request clarification or add additional context in comments.

3 Comments

work like a charm! thanks :) i'm so eager to learn regEx - it's a life saver
Yeah, regex is awesome! If you are looking for some resources, I used this website when I started learning. RegexOne is another cool resource with some interactive lessons.
I'll check that out.. Cheers mate
1

I would probably do it in 2 steps. I would focus my regex on the quotation marks, something like /"[0-9,.$]"/g. I can't remember if you have to escape the . (like \.) inside []s. Then, after extracting the whole, original number, I would have other code to remove the comma, dollar sign, spaces, etc.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.