Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

javascript - Regular expression with the cyrillic alphabet

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(//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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 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(/[wu0430-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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...