简体   繁体   中英

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.
I tried the solution I found here: Validating user's UTF-8 name in Javascript

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

And it's not working because of "ł" letter
I tried 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.

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]? thats what i usually use if im looking only for letters

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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