Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

how about a character range like [a-Z]? thats what i usually use if im looking only for letters

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top