Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
}
like image 836
fallco shkoder Avatar asked Sep 06 '25 22:09

fallco shkoder


1 Answers

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
like image 180
Dacre Denny Avatar answered Sep 08 '25 11:09

Dacre Denny