开发者

Difficulties with constructing this JavaScript regex

开发者 https://www.devze.com 2023-02-10 12:47 出处:网络
I would like to construct a regular expression that matches any letter (including accented and Greek), number, hyphens and spaces with a total allowed characters length between 3 and 50.

I would like to construct a regular expression that matches any letter (including accented and Greek), number, hyphens and spaces with a total allowed characters length between 3 and 50.

This is what I made:

[- a-zA-Z0-9çæœáééíóúžàèìòùäëïöüÿâêîôûãñõåøαβγδεζηθικλμνξοπρστυφχψωÇÆŒÁÉÍÓÚŽÀÈÌÒÙÄËÏÖÜŸÂÊÎÔÛÃÑÕÅØΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΤΥΦΧΨΩ]{3,50}

Now I wan't to adjust the expression so that it can't start with a hyphen or space. It will be used to validate a username.

I thought about usin开发者_如何学Cg a negative lookbehind but these are the limitations:

  • JavaScript doesn't support a lookbehind.
  • The alternatives for a lookbehind aren't really applicable since they all depend on other JavaScript functions and I am bound to using the match function.

I hope there are any regular expression heroes here since it doesn't look simple.


I replaced your long character class with a-z for readability:

[a-z][- a-z]{2,49}

You could also match with your current regex and then make sure that the string does not match ^[ -] in another match.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号