脚本数据库中的逗号

时间:2015-09-04 08:14:15

标签: jquery regex file

我有这个剧本:

$.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

虽然逗号不是分词符,但是脚本理解它们是,并且在第二行的逗号“,”之后读取错误,因为它将总线等理解为项目。有什么建议吗?

1 个答案:

答案 0 :(得分:1)

只需从reg-exp x = x.split(/[\;\n]+/);中删除逗号,因为正则表达式会根据;,分割字符串。

以下是更正后的代码

JS CODE:

$.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');