Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex in javascript working with Cyrillic (Russian) set

Is it possible to work with Russian characters, in javascript's regex?
Maybe the use of \p{Cyrillic}?

If yes, please provide a basic example of usage.

The example:

var str1 = "абв прв фву";
var regexp = new RegExp("[вф]\\b", "g");

 alert(str1.replace(regexp, "X"));

I expect to get: абX прX

like image 448
samuel Avatar asked Dec 31 '09 16:12

samuel


3 Answers

It should work if you just save the JavaScript file in UTF8. Then you should be able to enter any character in a string.

edit: Just made a quick example with some cryllic characters from Wikipedia:

var cryllic = 'абвгдеёжзийклмнопрстуфхцчшщъыьэюяабвгдеёжзийклмнопрстуфхцчшщъыьэюя';
cryllic.match( 'л.+а' )[0];
// returns as expected: "лмнопрстуфхцчшщъыьэюяа"
like image 103
poke Avatar answered Nov 16 '22 10:11

poke


Here is a good article on JavaScript regular expressions and unicode. Strings in JavaScript are 16 bit, so strings and RegExp objects can contain unicode characters, but most of the special characters like '\b', '\d', '\w' only support ascii. So your regular expression does not work as expected due to the use of '\b'. It seems you'll have to find a different way to detect word boundaries.

like image 22
Annie Avatar answered Nov 16 '22 08:11

Annie


According to this:

JavaScript, which does not offer any Unicode support through its RegExp class, does support \uFFFF for matching a single Unicode code point as part of its string syntax.

so you can at least use code points, but seemingly nothing more (no classes).

Also check out this duplicate of your question.

like image 1
Pekka Avatar answered Nov 16 '22 10:11

Pekka