Javascript RegExp.test allways returns true

后端 未结 2 1144
暗喜
暗喜 2021-01-24 11:30

I would like to create a javascript regex to test a string that would accept only characters from 0 to 9, a to z, A to Z and the followings chars: + * . for a total length betwe

相关标签:
2条回答
  • 2021-01-24 12:12

    If you don't "anchor" the regular expression to indicate that matches should start at the beginning and end at the end of the test string, then that is taken to mean that you want to see if the pattern can be found anywhere in the string.

    var reg = /^[0-9A-Za-z\+\*\.]{1,12}$/;
    

    With ^ at the beginning and $ at the end, you indicate that the entire string must match the pattern; that is, that no characters appear in the string other than those that contribute to the match.

    0 讨论(0)
  • 2021-01-24 12:12

    You're not setting it to match the start and end:

    var reg = /^[0-9A-Za-z\+\*\.]{1,10}$/;
    
    0 讨论(0)
提交回复
热议问题