-1

I'm trying to find a regex that checks if a string contains a valid JavaScript number.

So far i got this /(\d|\.\d|e\d)+/i. It's almost what I need but not completely. It now checks for a digit, a decimal point followed by a digit, or an case-insensitive e followed by a digit. However the regex should also do these things:

  • Stop when it reaches e or E for the second time
  • Numbers can't start with e or E (a decimal point at the start is okay)
  • Stop when it reaches a second decimal point or a decimal point after an e or E
  • A sign for negative numbers is only allowed at the start and/or directly after e or E

A few examples:

124e-10.3   =>  124e-10
-e-432.4.6  =>  -432.4
-.955e10e5  =>  -.955e10

I have no idea now to implement this in a regex... Or is there just a special regex 'token' to check if something is a number? By the way, if I haven't listed something that is also a condition for a JavaScript number, feel free to tell me in the comments or in your answer.

Sacha
  • 819
  • 9
  • 27
  • Possible duplicate of [Validate decimal numbers in JavaScript - IsNumeric()](https://stackoverflow.com/questions/18082/validate-decimal-numbers-in-javascript-isnumeric) – Poul Bak Aug 07 '18 at 18:50
  • @PoulBak That question is to validate strings that only contain the number, here it's about finding a number in a piece of text. Also the desired output in that question is either `true` or `false`, here it's the number itself. – Sacha Aug 07 '18 at 18:57
  • Downvote for you .. sorry but you chose a regex that won't parse `123.` as valid. –  Aug 07 '18 at 18:59

2 Answers2

1

You may use this regex to get your validated input:

let arr = ['124e-10.3', '-e-432.4.6', '-.955e10e5', '124.e-10.3']
let regex = /-?(?:\d+\.|\d*\.?\d+)(?:e-?\d+)?/i
let matches;

for (var i=0; i<arr.length; i++) {
  matches = regex.exec(arr[i])
  if (matches !== null)
    console.log(matches[0])
}

//=> 124e-10, -432.4, -.955e10, 124.e-10

RegEx Description:

  • -?: Match optional - at start
  • (?:\d+\.|\d*\.?\d+): Match a float number or an integer number
  • (?:e-?\d+)?: Match optional part with e or E
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • How do you get it to match `123.` ? –  Aug 07 '18 at 18:55
  • 1
    All languages parse `123.` is a valid float. So, how do you get a valid float like that to match with your regex ? –  Aug 07 '18 at 18:57
  • 1
    `123.` is the same as `123` in javascript, everything is of type `number`. `123 === 123.` returns true. For that reason it doesn't really matter whether the regex gets `123` or `123.` out of it. – Sacha Aug 07 '18 at 19:00
1

Works : -?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE]-?\d+)?

Readable version

 -?                     # Optional sign
 (?:                    # Number
      \d+ 
      (?: \. \d* )?
   |  \. \d+ 
 )
 (?: [eE] -? \d+ )?     # Optional  exponent
  • yes, maybe you're right. The one from anubhava doesn't match 123.e10 as 123.e10 but as 123... I think yours is more complete then. – Sacha Aug 07 '18 at 19:03