jquery-textcomplete不使用Unicode字符,并且缺少空格 [英] jquery-textcomplete not working with Unicode characters, and missing space
问题描述
1。我正在尝试使用带有Unicode字符串数组的jquery-textcomplete。当我输入英文单词时,它可以正常工作,但不建议使用Unicode单词。我认为问题出在'term'上。检查以下代码,请帮助我:
1. I'm trying to use jquery-textcomplete with a Unicode string array. When I type an English word, it works properly, but no Unicode words are suggested. I think the problem is with the 'term'. Check the below code and please help me:
var words = ['සහන', 'වනක', 'google', 'suresh namal', 'facebook', 'github', 'microsoft', 'yahoo', 'stackoverflow'];
$('textarea').textcomplete([{
match: /(^|\s)(\w{2,})$/,
search: function (term, callback) {
callback($.map(words, function (word) {
return word.indexOf(term) === 0 ? word : null;
}));
},
replace: function (word) {
return word + ' ';
}
}]);
2。此外,返回键存在问题。当我在'stackoverflow'之后输入'google'时,它会显示为'stackoverflowgoogle'。 'stackoverflow'和'google'之间没有空格。我怎么解决这个问题?谢谢。
2. Also, there is a problem with the return key. When I type 'google' after 'stackoverflow' it appears like 'stackoverflowgoogle'. There is no space between 'stackoverflow' and 'google'. How can I solve that? Thanks.
推荐答案
-
问题在于你的
匹配
选项,其中只匹配拉丁文字(\w
)
The problem is with your
match
option, where only the latin words are matched (\w
)
\w
匹配包括下划线在内的任何字母数字字符。相当于[A-Za-z0-9 _]。
\w
matches any alphanumeric character including the underscore. Equivalent to [A-Za-z0-9_].
您还应该包含unicode字符在您的RegExp中,例如: \\\ -\\\
You should also include unicode characters in your RegExp, like: \u0000-\u007f
-
这是因为使用
\s
(注意小的s)在你的RegExp中,它也取代了关键字前面的'空格':
this is because of using
\s
(notice small "s") in your RegExp, which also replaces a 'space' preceding the keyword:
\
匹配单个空白字符,包括空格(...)
\s
matches a single white space character, including space (...)
您可以使用 \S
(大写S)匹配单个字符其他比空间,还有一个 *
(星号)与前面的空格匹配0次或更多次。
You could use there \S
(capital "S") which matches a single character other than space, along with a *
(asterisk) which matches the preceding space 0 or more times.
它可能不是最漂亮的RegExp,但应该为你完成这项工作:
It's probably not a prettiest RegExp, but should do the job for you:
$('textarea').textcomplete([{
match: /(^|\S*)([^\u0000-\u007f]{2,}|\w{2,})$/,
search: function (term, callback) {
callback($.map(words, function (word) {
return word.indexOf(term) > -1 ? word : null;
}));
},
replace: function (word) {
return word+' ';
}
}]);
这篇关于jquery-textcomplete不使用Unicode字符,并且缺少空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!