Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Commas in script database

Tags:

jquery

regex

file

I've got this script:

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

I want it to read the first column of this 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

Though commas are not spliters, the script understands they are, and after the commas "," of the second line the reading is mistaken, as it understand bus, etc as items. Any suggestion?

like image 872
Kathlyn Avatar asked Feb 14 '26 09:02

Kathlyn


1 Answers

Just remove the comma from the reg-exp x = x.split(/[\;\n]+/); because your regular-expression is splitting the string based on both ; &,.

Below is the corrected code

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');
like image 106
dreamweiver Avatar answered Feb 17 '26 02:02

dreamweiver



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!