我有这个脚本:
$.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');
我想让它读取这个文件的第一列。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
虽然逗号不是分隔符,但脚本理解它们是分隔符,并且在第二行的逗号","之后,读取是错误的,因为它将bus等理解为项。任何建议吗?
只是从regg -exp 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');