exclude underscore from alpha numeric regex

前端 未结 5 1563
轮回少年
轮回少年 2020-12-10 11:47

I want use \\w regex for to allow alpha numeric but I don\'t want underscore _ to be part of it. Since _ is included in \\w

5条回答
  •  独厮守ぢ
    2020-12-10 12:21

    Your proposed solution:

    (/^roger\w{2,3}[0-9a-z]/i)
    

    Means:

    \w{2,3} -- 2 or 3 alphanumeric, including the _

    [0-9a-z] (with the /i) -- a single character that is alphanumeric, not including the _

    I didn't see any mention of the acceptable 3 alphanumerics at the beginning. Does that belong?

    Both "roger54" and "roger4a" should fail this because the above regex requires at least three characters following "roger." Likewise, "roger_a" would succeed because "_" passes \w{2,3} (specifically \w{3}).

    Your request sounded like you wanted more of one of these:

    /^roger[0-9a-z]+/i
    /^roger[0-9a-z]*/i
    

    that is, "roger" (case insensitive) followed by one or more (+) or zero or more (*) letters and/or numbers.

提交回复
热议问题