Reputation: 468
I had to rewrite this regular expression for Unicode (utf-8, cyrillic):
match: /\b(\w{2,})$/,
Using this regular expressions:
(/[\wа-я]+/ig)
(/[\w\u0430-\u044f]+/ig)
I rewrote this way:
match: /\b(\wа-яa-z{2,})+/ig$/
But my reg.exp code not working. Please help me. Full code:
$('.form-control').textcomplete([
{
words: ["россия","сша","англия","германия","google","git","github","php","microsoft","jquery"],
match: /(?:^|[^\wа-я])([\wа-я]{2,})$/i,
search: function (term, callback)
{
callback($.map(this.words, function (word) {return word.indexOf(term) === 0 ? word : null;}));
},
index: 1,replace: function (word) {return word + ' ';}
}]);
Upvotes: 4
Views: 116
Reputation: 626845
You need to use
$('.form-control').textcomplete([
{
words: ["россия","сша","англия","германия","google","git","github","php","microsoft","jquery"],
match: /(^|[^\wа-яё])([\wа-яё]{2,})$/i,
search: function (term, callback)
{
callback($.map(this.words, function (word) {return word.indexOf(term) === 0 ? word : null;}));
},
index: 2, // THIS IS A DEFAULT VALUE
replace: function (word) {return '$1' + word + ' ';}
}]);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.2/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.textcomplete/0.2.2/jquery.textcomplete.min.js"></script>
<textarea class="form-control" rows=5 cols=50></textarea>
The pattern (^|[^\wа-я])([\wа-я]{2,})$
works as follows:
(^|[^\wа-яё])
- Capturing group 1: start of string or any char other than a word and Russian letters([\wа-яё]{2,})
- Capturing group 2: 2 or more word or Russian letters$
- end of string.NOTE:
$1
inside the replace
(see this source code showing that all the literal $n
backreferences are replaced with match[n]
)return '$1' + word + ' ';
index: 2
since this value will be handled as the termё
to the character classes since the [а-я]
range does not include itindex
value is set to 2
by default, thus, you may remove index: 2
from the code above.Upvotes: 3