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 + ' ';
}
}]);
JS Fiddle
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.
The problem is with your match option, where only the latin words are matched (\w)
\wmatches any alphanumeric character including the underscore. Equivalent to [A-Za-z0-9_].
Reference
You should also include unicode characters in your RegExp, like: \u0000-\u007f
this is because of using \s (notice small "s") in your RegExp, which also replaces a 'space' preceding the keyword:
\smatches a single white space character, including space (...)
Reference
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.
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+' ';
}
}]);
JSFiddle
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With