我有这个脚本:
$.get('file.txt', function(x) {
var i;
var pos = 0;
var availableTags = [];
x = x.split(/[\;,\n]+/);
for (i = 0; i < x.length; i = i + 2)
availableTags[pos++] = x[i];
console.log(availableTags);
$(function() {
$("#search").autocomplete({
source: availableTags
});
});
}, 'text');
我希望它阅读此file.txt的第一列
Supermarket;big shop where a wide range of products is sold
Station;a place where you can take a train, a bus, etc.
School;place where students learn
尽管逗号不是分隔符,但脚本知道它们是分隔符,并且在第二行的逗号“,”之后,由于将总线等理解为项,因此读取是错误的。有什么建议吗?
最佳答案
只需从正则表达式x = x.split(/[\;\n]+/);
中删除逗号,因为您的正则表达式基于;
和,
拆分字符串。
下面是更正的代码
JS代码:
$.get('file.txt', function(x) {
var i;
var pos = 0;
var availableTags = [];
x = x.split(/[\;\n]+/); //removed ',' from regular-expression
for (i = 0; i < x.length; i = i + 2){
availableTags[pos++] = x[i];
}
console.log(availableTags);
$(function() {
$("#search").autocomplete({
source: availableTags
});
});
}, 'text');