I need a function to remove all characters except numbers + characters: ‘$’, ‘.’ and ‘,’.
How can I do this?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This is the answer you asked for. I would not recommend it for extracting currencies, since it can suffer from problems like this:
If you want to just extract expressions of a currency-like form, you could do:
If you need more complicated matching (e.g. you’d just like to extract the dollar value and ignore the cent value) you could do something like How do you access the matched groups in a JavaScript regular expression? for example:
(Thus be careful, javascript regexp objects are not immutable/final objects, but have state and can be used for iteration as demonstrated above. You thus cannot “reuse” a regexp object. Even passing
myRegex2 = RegExp(myRegex)will mix state; a very poor language decision for the constructor. See the addendum on how to properly clone regexes in javascript.) You can rewrite the above as a very exotic for-loop if you’d like:addendum – Why you can’t reuse javascript RegExp objects
Bad language design, demonstrating how state is reused:
How to properly clone a regex in javascript (you have to define it with a string):
If you wish to programmatically preserve flags such as
'g', you can probably useregexTemplate = ['(x.)', 'g']; RegExp.apply(this, regexTemplate).