jquery-textcomplete not working with Unicode chara

2019-05-11 01:38发布

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.

1条回答
ら.Afraid
2楼-- · 2019-05-11 01:55
  1. The problem is with your match option, where only the latin words are matched (\w)

    \w matches 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


  1. 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 (...)

    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

查看更多
登录 后发表回答