简体   繁体   English

使用XRegExp验证JavaScript(Node.js)中的UTF-8名称

[英]Validating UTF-8 names in JavaScript (Node.js) with XRegExp

I'm trying to check if a given UTF-8 string consists of letters only. 我正在尝试检查给定的UTF-8字符串是否仅包含字母。
I tried the solution I found here: Validating user's UTF-8 name in Javascript 我尝试了在这里找到的解决方案: 在Javascript中验证用户的UTF-8名称

Given string: Ciesiołkiewicz 给定字符串: Ciesiołkiewicz
is tested with var XRegExp = require('xregexp').XRegExp('^\\\\p{L}+$'); var XRegExp = require('xregexp').XRegExp('^\\\\p{L}+$');

And it's not working because of "ł" letter 而且由于“ł”字母而无法正常工作
I tried XRegExp('^[\\\\p{Latin}\\\\p{Common}]+$'); 我尝试了XRegExp('^[\\\\p{Latin}\\\\p{Common}]+$');
but it's too much, it accepts polish letters but also characters like "$" etc 但是它太多了,它接受波兰字母,但也可以接受“ $”等字符

How can I validate it against letters only? 如何仅针对字母验证它? I don't want to type them into regexp manually. 我不想手动将它们输入到regexp中。

var XRegExp = require('xregexp').XRegExp;
var re = new XRegExp('^\\p{L}+$');

console.log(re.test('Ciesiołkiewicz'));
console.log(re.test('1Ciesiołkiewicz2'));
console.log(re.test('привет'));
console.log(re.test('пр1вет'));

> true
> false
> true
> false

works perfectly. 完美地工作。

how about a character range like [aZ]? 像[aZ]这样的字符范围如何? thats what i usually use if im looking only for letters 如果我只寻找字母,多数民众赞成在我通常使用的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM