I’ve retrieved a string into a variable with innerHTML method. The string is:
£ 125.00
<!-- End: pagecomponent/pricesplit -->
I only want the 125.00 part. Is it possible to use the parseInt() method to convert this into an integer? Alternatively what can I do to extract the 125.00 part?
Thanks
You probably want to use a regular expression to remove anything but digits and periods, and then run
parseInton the remaining string:Test the regex here. Here’s a breakdown:
/.../is just the syntax you use for wrapping a regular expression. Ignore these.[...]creates a character class.^when placed at the beginning of a character class, it negates everything inside.\dany digits.a literal period. It does not need to be escaped inside a character class – outside it would mean “any character” and need to be escaped.So
/[^\d.]/means “match anything that is not a digit or period”, and subsequently replace it with an empty string.If your number might include significant digits after the decimal, such as
125.50, you should useparseFloatinstead ofparseInt.