最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to allow regular letterscharacters of all languages (in a string) while prevent special characters - Stack Over

programmeradmin0浏览0评论

First of all this is not a duplicate (as far as I know).

What I exactly want is to allow the user to have a username (profile name) that contains only valid characters, which in my case, are letters of all languages, as well as spaces. But at the same time prevent numbers, symbols (like !@#$%^&*()|\/?'";:=+-_.<>,~), other unmon symbols (like ©®♣♥♠♩¸¸♪·¯·♫), new-line, tab, and similar characters, emojis, and every single character that is not normal to be seen in a name...

Well, to make it clearer, I want to implement the exact same profile name system as Facebook for example.

I'm using JS (Node), and I tried regular expressions so far, but I don't thing it's sane to type every single range of valid characters in unicode in that expression is it?! I'll not even try thinking about what would that cause for me in future when I need to edit those ranges...

Is there any libraries that provide a way to do that exactly? If no, what other options do I have?

Any help is appreciated!

First of all this is not a duplicate (as far as I know).

What I exactly want is to allow the user to have a username (profile name) that contains only valid characters, which in my case, are letters of all languages, as well as spaces. But at the same time prevent numbers, symbols (like !@#$%^&*()|\/?'";:=+-_.<>,~), other unmon symbols (like ©®♣♥♠♩¸¸♪·¯·♫), new-line, tab, and similar characters, emojis, and every single character that is not normal to be seen in a name...

Well, to make it clearer, I want to implement the exact same profile name system as Facebook for example.

I'm using JS (Node), and I tried regular expressions so far, but I don't thing it's sane to type every single range of valid characters in unicode in that expression is it?! I'll not even try thinking about what would that cause for me in future when I need to edit those ranges...

Is there any libraries that provide a way to do that exactly? If no, what other options do I have?

Any help is appreciated!

Share Improve this question asked Feb 24, 2021 at 9:07 user152352user152352 791 silver badge5 bronze badges 2
  • 1 "unmon symbols" and "normal to be seen in a name" are subjective. – adiga Commented Feb 24, 2021 at 9:11
  • 1 @adiga Yep, that's why I said, exactly like Facebook :) – user152352 Commented Feb 24, 2021 at 9:13
Add a ment  | 

1 Answer 1

Reset to default 7

For English only you could use a simple character class such as /^[a-zA-Z ]$/ or with word char /^[\w ]$/. There is a Unicode equivalent for this:

/^[\p{L}\p{M}\p{Zs}]{2,30}$/u

Explanation:

  • enable Unicode with the 'u' flag
  • \p{L} - denotes a letter char in any language
  • \p{M} - denotes a mark (accent etc)
  • \p{Zs} - denotes a space char, such as regular space and Japanese space char  

In case you want to prevent space at the start and end, use these negative lookaheads:

/^(?!\p{Zs})(?!.*\p{Zs}$)[\p{L}\p{M}\p{Zs}]{2,30}$/u

Example function:

function validateName(name) {
  return /^[\p{L}\p{M}\p{Zs}]{2,30}$/u.test(name);
}

See a demo at https://regex101./r/A4QDIf/1

See docs on Unicode regex: https://javascript.info/regexp-unicode

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论