Javascript regex - no white space at beginning + allow space in the middle

后端 未结 8 1704
故里飘歌
故里飘歌 2020-12-16 15:15

I would like to have a regex which matches the string with NO whitespace(s) at the beginning. But the string containing whitespace(s) in the middle CAN match. So far i have

相关标签:
8条回答
  • 2020-12-16 15:53

    If your field for user name only accept letters and middle of space but not for begining and end

    User name: /^[^\s][a-zA-Z\s]+[^\s]$/

    If your field for user ID only accept letters,numbers and underscore and no spaces allow

    user ID: /^[\w]+$/

    If your field for password only accept letters,number and special character no spaces allow

    Password: /^[\w@#&]+$/

    Note: \w content a-zA-Z, number, underscore (_) if you add more character, add you special character after \w.

    You can compare with user ID and password field in password field im only add some special character (@#&).

    India public thoko like

    0 讨论(0)
  • 2020-12-16 15:56

    In your 2nd character class, \\s will match \ and s, and not \s. Thus it doesn't matches a whitespace. You should use just \s there. Also, move the hyphen towards the end, else it will create unintentional range in character class:

    ^[^-\s][a-zA-Z0-9_\s-]+$
    
    0 讨论(0)
提交回复
热议问题