John
John

Reputation: 468

My regular expression code not working

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

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

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:

  • We need both capturing groups to be able to keep the non-word char or start of string before the match we need to modify
  • To keep the char that can be matched and captured into Group 1 should be restored with the $1 inside the replace (see this source code showing that all the literal $n backreferences are replaced with match[n])
  • Thus, we no longer need to replace with double spaces at the end, use return '$1' + word + ' ';
  • We need to use index: 2 since this value will be handled as the term
  • To match all Russian letters, you need to add ё to the character classes since the [а-я] range does not include it
  • Also, the index value is set to 2 by default, thus, you may remove index: 2 from the code above.

Upvotes: 3

Related Questions