+ 1
[Solved] Why this regular expression doesn't work on specific cases
From prismJS I got a regex that matches all types of numbers. I modified it to match the first occurrence of the number. But it doesnt work for the number with underscores (like 2_4_4, which is valid) RegExp with test cases: https://code.sololearn.com/Wa5A5A5a09a4 What is the problem with this regex? (this regex is too long to read. There is a visual representation of this here: https://tinyurl.com/8efuv5jf )
17 Answers
+ 2
after digit_digit the loop to _ is needed but missing.
+ 2
it allows one underscore.
2_4 is valid.
+ 2
on the bottom of your graphics I see u need
2_4
and then
a dot 2_4.2_4
or again n_n
2_46_8
why u need a dot after 2_4?
replace by - could help.
+ 2
That was fun. Congrats
+ 1
Oma Falk 34_5_.3 is not a valid number in JS. There should be atleast one digit after underscore.
0
Oma Falk that's the problem. It should match all of them. The output should be 2_3_4 not 2_3
0
Oma Falk I think you misunderstood. I'm creating a parser. And I will have input like `2_5_6 + 7` and I need a regex that returns the first number of the input (that is 2_5_6).
So that would be like this:
```
var input = "2_5_6 + 7"
var firstNumber = input.match(numberFinderRegExp)[0];
console.log(firstNumber)// should return '2_5_6'
```
0
It seems that your solution solves it. But it doesn't work on decimal number sequences.
If the test case was "335_772_37.62_727 + 6", it only returns "335_772_37" whereas it should return "335_772_37.62_727"
0
Oma Falk I've updated the test code check it again
0
but now
1_2.3_4.5_6 is valid
ohhh it Red. not valid.
0
Oma Falk all the given test cases are valid JS numbers. The green colour say that the regex matched every character of the test. While red indicate that regex failed to match all characters in the test
0
It doesn't check if the number is valid but it search for the valid ones. In the code all give test cases are valid
0
Oma Falk I actually got a solution. As you said after digit_digit there should be a loop to _ again. I added it. Thanks for clue
0
Solution: https://tinyurl.com/d7zh4ah5 .
I don't know how concrete the solution is, but will be ready to encounter flaws in future
0
Oma Falk welcome