search

Home  >  Q&A  >  body text

js regular expression {} range - Stack Overflow

Regular matching: starting with a lowercase letter, ending with a lowercase letter, and there can be a dash (-) in the middle with a length less than 32 characters

var ss = /^[a-z]+([A-Za-z0-9_]|(-?)|[a-z]+$){1,31}/;
str="ss-fjdkkldjkskjdskjdksjdkjdkjklskssdkdkffdkdfkdfk";
ss.test(str);

The result returned is
true
Normally it should return false.
{1,31} means that the range before matching is between 1 and 31. Why is the matching incorrect?

黄舟黄舟2805 days ago1005

reply all(2)I'll reply

  • 怪我咯

    怪我咯2017-06-12 09:34:53

    My understanding of this regular expression is that it starts with the letters a-z, and then the content in () appears 1 to 31 times.
    () means A-Z a-z 0-9 _ - any one of them can appear >= 1 time

    reply
    0
  • 淡淡烟草味

    淡淡烟草味2017-06-12 09:34:53

    The regular expression can be written like this

    /^(?=.{1,32}$)[a-z]\w*(?:-\w*[a-z])?$/

    reply
    0
  • Cancelreply