My regex code is not working

I had to rewrite this regex for Unicode (utf-8, cyrillic):

match: /\b(\w{2,})$/, 

Using these regular expressions:

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

I rewrote this path:

 match: /\b(\w-a-z{2,})+/ig$/ 

But my reg.exp code is 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 + ' ';} }]); 
+4
source share
1 answer

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-]) - Capture of group 1: the beginning of a line or any char, except for a word and Russian letters
  • ([\w-]{2,}) - Capture a group of 2: 2 or more words or Russian letters
  • $ is the end of the line.

Note

  • We need both capture groups in order to be able to save the non-word char or start a line before the match, which we need to change
  • To save a char that can be mapped and written to group 1, you need to restore it with $1 inside replace (see this source code showing that all literal backlinks of $n are replaced with match[n] )
  • Thus, we no longer need to replace double spaces at the end, use return '$1' + word + ' ';
  • We need to use index: 2 , since this value will be treated as a term
  • To match all Russian letters, you need to add to the character classes, since the range [-] does not include it
  • The default value for index is 2 , so you can remove index: 2 from the code above.
+3
source

Source: https://habr.com/ru/post/1272181/


All Articles