我正在检查输入字符串,该字符串只能包含数字和可选的单斜杠(不应位于开始或结束位置)。
123456789 // valid
3221345431 // valid
23243245/34 // valid
454556/322 // valid
45435//564 // invalid as more than one /
454354356/ // invalid as / is at end position
/454354354 // invalid as / is not start position
我尝试使用RegExp
,但没有达到预期的效果。
> re = RegExp('\\d+\/?\\d+')
/\d+\/?\d+/
> re.test('12791771/21')
true // This is correct
> re.test('477199337932')
true // This is correct
> re.test('12200977/')
true // This is wrong because / is at end position
> re.test('122009//77')
true // This is wrong because more than one /
> re.test('/12200977')
true // This is wrong because / is at start position
我做错了什么,所以在最近三种情况下没有得到false
?