Search code examples
javascriptregexregex-group

Find Regex for `1.5×10^9` & `4.75×10^9` in JavaScript?


I am trying to find a Regex for 1.5×10^9. Another value I have is 4.75×10^9

I have been able to make it work for 1.5×10^9 using /(\d.\d)×10\^(\d)/ but that doesn't work for 4.75×10^9.

Especially the first group is of decimal numbers & I'm not able to put Regex for decimal numbers inside the first parenthesis ().

How do I do it?

I want only 2 values: the first decimal value & the one after caret ^


Solution

  • Used Wiktor Stribizew's suggestion in the comments below:

    /(\d+(?:\.\d+)?)×10\^(\d+)/ // matches 1.5×10^9 & 4.75×10^9
    

    This works for my current use case but a more robust solution for decimal places can be found here