js regular expression {} range - Stack Overflow
黄舟
黄舟 2017-06-12 09:32:53
0
2
948

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?

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

reply all(2)
刘奇

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

淡淡烟草味

The regular expression can be written like this

/^(?=.{1,32}$)[a-z]\w*(?:-\w*[a-z])?$/
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template