0

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 ^

deadcoder0904
  • 7,232
  • 12
  • 66
  • 163

1 Answers1

0

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

deadcoder0904
  • 7,232
  • 12
  • 66
  • 163
  • 1
    You have to escape the dot `\.` as it has a special meaning in regex. – Toto Apr 03 '21 at 09:43
  • 1
    Also, you need to use `+` with all `\d`, and probably allow integers as well by adding an optional group, `/(\d+(?:\.\d+)?)×10\^(\d+)/` – Wiktor Stribiżew Apr 03 '21 at 10:53
  • Thanks, @WiktorStribiżew, I think your code looks perfect for my use case. I hadn't considered 2-digit numbers after the ^ sign. Good point on supporting integers as well. – deadcoder0904 Apr 04 '21 at 10:19