Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression with the cyrillic alphabet

Tags:

I have an jQuery function for word counting in textarea field. In addition its excludes all words, which are closed in [[[tripple bracket]]]. It works great with latin character, but it has a problem with cyrillic sentences. I suppose that the error is in part with regular expression:

$(field).val().replace(/\[\[\[[^\]]*\]\]\]/g, '').match(/\b/g); 

Example with both kind of phrases: http://jsfiddle.net/A3cEG/2/

I need count all word, including cirillic expressions, not only words in latin. How to do that?

like image 838
Jiří Valoušek Avatar asked Aug 27 '13 16:08

Jiří Valoušek


People also ask

What is '?' In regular expression?

means "zero or one digits, but not two or more". [0-9]* means "zero or more digits (no limit, could be 42 of them)". Note that some languages require that floats are written with a leading 0 before the . if the number is between 0 and 1 ( 0.5 not .

Does regex work with Unicode?

RegexBuddy's regex engine is fully Unicode-based starting with version 2.0. 0.

What characters are allowed in regex?

1.1 Regex Syntax Summary. Character: All characters, except those having special meaning in regex, matches themselves. E.g., the regex x matches substring "x" ; regex 9 matches "9" ; regex = matches "=" ; and regex @ matches "@" .

What is example of regular expression?

Solution: As we know, any number of a's means a* any number of b's means b*, any number of c's means c*. Since as given in problem statement, b's appear after a's and c's appear after b's. So the regular expression could be: R = a* b* c*


2 Answers

JavaScript (at least the versions most widely used) does not fully support Unicode. That is to say, \w matches only Latin letters, decimal digits, and underscores ([a-zA-Z0-9_]), and \b matches the boundary the between a word character and and a non-word character.

To find all words in an input string using Latin or Cyrillic, you'd have to do something like this:

.match(/[\wа-я]+/ig); // where а is the Cyrillic а. 

Or if you prefer:

.match(/[\w\u0430-\u044f]+/ig); 

Of course this will probably mean you need to tweak your code a little bit, since here it will match all words rather than word boundaries. Note that [а-я] matches any letter in the 'basic Cyrillic alphabet' as described here. To match letters outside of this range, you can modify the character set as necessary to include those letters, e.g. to also match the Russian Ё/ё, use [а-яё].

Also note that your triple-bracket pattern can be simplified to:

.replace(/\[{3}[^]]*]{3}/g, '') 

Alternatively, you might want to look at the XRegExp project—which is an open-source project to add new features to the base JavaScript regular expression engine—and its Unicode addon.

like image 94
p.s.w.g Avatar answered Oct 06 '22 02:10

p.s.w.g


Beware of using range of cyrillic letters, it may contain unnecessary characters within. There is bulletproof regexp contains only cyrillic letters:

/^[аАбБвВгГдДеЕёЁжЖзЗиИйЙкКлЛмМнНоОпПрРсСтТуУфФхХцЦчЧшШщЩъЪыЫьЬэЭюЮяЯ]+$/ 
like image 30
Dubaua Avatar answered Oct 06 '22 04:10

Dubaua