javascript - Regexp for floating number -


I found this regexp to validate the flag. But I can not see how 2-1 will be accepted. Assesses to the bottom right, I can not use parse float because I need to accept "" instead of "". "I also wrote 2 again, the same result though

  var re1 = new RegExp (" ^ [- +]? [0- 9] *. [0-9] + $ "); Console.log (re1.test (" 2-1 "); var re2 = new RegExp (" ^ ([0- 9] +) \. ([0- 9] +) $ "); console .log (re2.test ("2-1");    

If you If you generate regex using Constructor function, you have to save from backslash, i.e. \ becomes \\ :

  Var re1 = New RegExp ("^ [- +] [0-9] * * \ [[0-9] + $").?   

Another option is to use literal syntax Is to use There is no need to escape:

  var re1 = / ^ [- +] [0-9]] * \. [0-9] + $ /    

Comments