我不得不为Unicode(utf-8,cyrillic)重写这个正则表达式:
match: /\b(\w{2,})$/,
使用此正则表达式:
(/[\wа-я]+/ig)
(/[\w\u0430-\u044f]+/ig)
我用这种方式改写了:
match: /\b(\wа-яa-z{2,})+/ig$/
但我的reg.exp代码无效。请帮我。 完整代码:
$('.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 + ' ';}
}]);
答案 0 :(得分:3)
您需要使用
$('.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>
&#13;
模式(^|[^\wа-я])([\wа-я]{2,})$
的工作原理如下:
(^|[^\wа-яё])
- 捕获第1组:字符串的开头或除了单词和俄语字母之外的任何字符([\wа-яё]{2,})
- 捕获第2组:2个或更多单词或俄文字母$
- 字符串结束。注意强>:
$1
内的replace
进行恢复(请参阅this source code显示所有文字$n
反向引用将替换为match[n]
)return '$1' + word + ' ';
index: 2
,因为此值将作为术语ё
添加到字符类,因为[а-я]
范围不包含它index
值设置为2
,因此,您可以从上面的代码中移除index: 2
。