简体   繁体   中英

Javascript: Get first number substring for each semi-colon separated substring

I am creating a script of time calculation from MySQL as I don't want to load the scripts on server-side with PHP.

I am getting the data and parsing it using JSON, which gives me a string of values for column and row data. The format of this data looks like:

1548145153,1548145165,End,Day;1548145209,1548145215,End,Day;1548148072,1548148086,End,Day;1548161279,1548161294,End,Day;1548145161,1548145163,End,Day;1548148082,1548148083,End,Day;1548161291,1548161293,End,Day

I need to split this string by semi-colon, and then extract the first VARCHAR number from before each comma to use that in subsequent calculation.

So for example, I would like to extract the following from the data above:

[1548145153, 1548145209, 1548148072, 1548161279, 1548145161, 1548148082, 1548161291]

I used the following type of for-loop but is not working as I wanted to:

for (var i=0; i < words.length; i++) {
           var1 = words[i];
           console.log(var1);
}

The string and the for-loop together are like following:

var processData = function(data) {
for(var a = 0; a < data.length; a++) {

     var obj = data[a];
     var str= obj.report // something like 1548145153,1548145165,End,Day;1548145209,1548145215,End,Day;1548148072,1548148086,End,Day;1548161279,1548161294,End,Day;1548145161,1548145163,End,Day;1548148082,1548148083,End,Day;1548161291,1548161293,End,Day
     words = str.split(',');
     words = str.split(';');
     for (var i=0; i < words.length; i++) {
           var1 = words[i];
           var2 = var1[0];
           console.log(var2);
}

First thing is that you override words with the content of str.split(';') , so it won't hold what you expect. To split the string into chunks, split by ; first, then iterate over the resulting array and within the loop, split by , .

 const str= "1548145153,1548145165,End,Day;1548145209,1548145215,End,Day;1548148072,1548148086,End,Day;1548161279,1548161294,End,Day;1548145161,1548145163,End,Day;1548148082,1548148083,End,Day;1548161291,1548161293,End,Day"; const lines = str.split(';'); lines.forEach(line => { const parts = line.split(','); console.log(parts[0]); }); 

Here is an approach based on a regular expression:

 const str = "1548145153,1548145165,End,Day;1548145209,1548145215,End,Day;1548148072,1548148086,End,Day;1548161279,1548161294,End,Day;1548145161,1548145163,End,Day;1548148082,1548148083,End,Day;1548161291,1548161293,End,Day"; const ids = str.match(/(?<=;)(\\d+)|(^\\d+(?=,))/gi) console.log(ids) 

The general idea here is to classify the first VARCHAR value as either:

  • a number sequence directly preceded by a ; character (see 1 below) or, for the edge case
  • the very first number sequence of the input string directly followed by a , character (see 2 below).

These two cases are expressed as follows:

  1. Match any number sequence that is preceded by a ; using the negated lookbehind rule : (?<=;)(\\d+) , where ; is the character that must follow a number sequence \\d+ to be a match
  2. Match any number sequence that is the first number sequence of the input string, and that has a , directly following it using the lookahead rule (^\\d+(?=,)) , where \\d+ is the number sequence and , is the character that must directly follow that number sequence to be a match
  3. These building blocks 1 and 2 are combined using the | operator to achieve the final result

What you are doing is not correct, you'll have to separate strings twice as there are two separators. ie a comma and a semicolon.

I think you need a nested loop for that.

var str = "1548145153,1548145165,End,Day;1548145209,1548145215,End,Day;1548148072,1548148086,End,Day;1548161279,1548161294,End,Day;1548145161,1548145163,End,Day;1548148082,1548148083,End,Day;1548161291,1548161293,End,Day"
let words = str.split(';');
for (var i=0; i < words.length; i++) {
    let varChars = words[i].split(',');
    for (var j=0; j < varChars.length; i++)
        console.log(varChars[j]);
}

I hope this helps. Please don't forget to mark the answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM