我使用jquery-textcomplete对于我的文本区域。这是工作不正确。例如,第一步我将输入单词 apple
,然后添加空格,第二种方式添加单词 app
,并且此脚本或自动完成功能找不到单词 apple
>。为什么?如何纠正呢?为了进行测试,运行片段并写入单词 app
并显示单词 app
和 apple
。选择单词apple
,然后添加空格,然后添加单词app
,并且单词不会显示。然后在 app
之后添加 appe
并查看结果。
$('#textcomplete').textcomplete([{
words: ['тоҷик', 'оҷиз', 'ҷило', 'тоҷ', 'тоҷикистон', 'apple', 'please', 'app', 'perl', 'person', 'erlan'],
match: /(^|\S*)([^\u0000-\u007f]{2,}|\w{2,})$/,
search: function(term, callback) {
callback($.map(this.words, function(word) {
if (word.indexOf(term.toLocaleLowerCase()) !== 0)
return null;
if (term[term.length-1] === term[term.length-1].toLocaleUpperCase())
return word.toLocaleUpperCase(); // last char is upper = uppercase
if (term[0] === term[0].toLocaleUpperCase())
return word.charAt(0).toLocaleUpperCase() + word.slice(1); // first char is upper = capitalized
return word; // default; is lowercase
}));
},
index: 2,
replace: function(word) {
return word + ' ';
}
}]);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.textcomplete/1.8.4/jquery.textcomplete.min.js"></script>
<textarea id="textcomplete"></textarea>
最佳答案
新答案
我认为你的正则表达式的开头是不必要的。
如果我错了,请纠正我,但以下方法有效:
$('#textcomplete').textcomplete([{
words: ['тоҷик', 'оҷиз', 'ҷило', 'тоҷ', 'тоҷикистон', 'apple', 'please', 'app', 'perl', 'person', 'erlan'].sort(), // TAKIT: sort array
match: /([^\u0000-\u007f]{2,}|\w{2,})$/, // TAKIT: modified here
search: function(term, callback) {
callback($.map(this.words, function(word) {
if (word.indexOf(term.toLocaleLowerCase()) !== 0)
return null;
if (term[term.length-1] === term[term.length-1].toLocaleUpperCase())
return word.toLocaleUpperCase(); // last char is upper = uppercase
if (term[0] === term[0].toLocaleUpperCase())
return word.charAt(0).toLocaleUpperCase() + word.slice(1); // first char is upper = capitalized
return word; // default; is lowercase
}));
},
index: 1, // TAKIT: modified here
replace: function(word) {
return word + ' ';
}
}]);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.textcomplete/1.8.4/jquery.textcomplete.min.js"></script>
<textarea id="textcomplete"></textarea>
旧答案
(不适用于 unicode 字符)
由于我根本不是正则表达式专家,我承认我不明白问题到底是什么。
无论如何......我使用了与正则表达式中的“单词边界”匹配的 \b
元字符,而不是 ^|\S*
。
这只是一个简化,但问题似乎不再出现了:
$('#textcomplete').textcomplete([{
words: ['тоҷик', 'оҷиз', 'ҷило', 'тоҷ', 'тоҷикистон', 'apple', 'please', 'app', 'perl', 'person', 'erlan'],
match: /(\b)([^\u0000-\u007f]{2,}|\w{2,})$/,
search: function(term, callback) {
callback($.map(this.words, function(word) {
if (word.indexOf(term.toLocaleLowerCase()) !== 0)
return null;
if (term[term.length-1] === term[term.length-1].toLocaleUpperCase())
return word.toLocaleUpperCase(); // last char is upper = uppercase
if (term[0] === term[0].toLocaleUpperCase())
return word.charAt(0).toLocaleUpperCase() + word.slice(1); // first char is upper = capitalized
return word; // default; is lowercase
}));
},
index: 2,
replace: function(word) {
return word + ' ';
}
}]);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.textcomplete/1.8.4/jquery.textcomplete.min.js"></script>
<textarea id="textcomplete"></textarea>
希望有帮助。
关于javascript - 为什么 jquery textcomplete 在第一次输入单词后无法正确找到其他单词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50488563/