为什么在第一次输入单词后jquery textcomplete无法正确找到其他单词?

时间:2018-05-23 12:34:20

标签: javascript jquery regex utf-8 autocomplete

我将jquery-textcomplete用于我的textarea。这是不正确的工作。例如,第一步我将输入单词apple,在添加空格后,第二步添加单词app,此脚本或自动填充功能无法找到单词apple。为什么?怎么纠正呢?对于测试运行剪切和写单词app并显示单词appapple。选择单词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>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

新答案

我认为你的正则表达式的开头是必要的 如果我错了,请纠正我,但以下是有效的:

&#13;
&#13;
$('#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 + ' ';
  }
}]);
&#13;
<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>
&#13;
&#13;
&#13;

旧答案

(不使用unicode字符)

由于我根本不是RegEx专家,我承认我并不完全明白问题所在。
无论如何......我使用的\b元字符匹配&#34;字边界&#34;在正则表达式而不是^|\S* 它本来只是一种简化,但问题似乎不再出现了:

&#13;
&#13;
$('#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 + ' ';
  }
}]);
&#13;
<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>
&#13;
&#13;
&#13;

希望它有所帮助。