Skip to content
Advertisement

Align/pad some content in these strings with regex in javascript

I have these strings:

17/07/13     03:50:24     12.122N      88.214W     21     4.0     107 Km al suroeste de Boca del Padre Ramos
17/07/12     20:51:36     10.630N       6.634W     16     3.3     94 Km al noroeste de Tamarindo, Costa Rica
17/07/12     13:28:09     13.389N      87.986W     4     2.8     17 Km al noroeste de La Union, El Salvador
17/07/12     13:20:49     11.030N      85.177W     225     2.2     41 Km al sureste de Cárdenas
17/07/11     13:04:47     13.389N      87.981W     1     3.2     16 Km al noroeste de La Union, El Salvador
17/07/11     12:01:34      9.241N      83.983W     27     2.5     14 Km al oeste de Dominical, Costa Rica

and i want to convert them to this:

17/07/13     03:50:24     12.122N      88.214W      21     4.0     107 Km al suroeste de Boca del Padre Ramos
17/07/12     20:51:36     10.630N       6.634W      16     3.3      94 Km al noroeste de Tamarindo, Costa Rica
17/07/12     13:28:09     13.389N      87.986W       4     2.8      17 Km al noroeste de La Union, El Salvador
17/07/12     13:20:49     11.030N      85.177W     225     2.2      41 Km al sureste de Cárdenas
17/07/11     13:04:47     13.389N      87.981W       1     3.2      16 Km al noroeste de La Union, El Salvador
17/07/11     12:01:34      9.241N      83.983W      27     2.5      14 Km al oeste de Dominical, Costa Rica

Want to do it with Regular Expressions in Javascript.

NOTE 1: The goal is to align the data in the “fifth, sixth and seven column” from right to left.

The fifth column has from 1 to 3 digits.

The sixth column always has 1 digit, a dot and another digit.

The seven column has digits from 1 to 3 digits, the phrase ‘km al’, and other words.

NOTE 2: Every line is independent (i have each one in an array) I put more than 1 line to show the differents kinds of scenarios, because finally i need to print out all the lines.

EDIT (to avoid the question to be closed):

I have tried this:

line.replace(/^((?:s*S+){5})s+?([sd]{5}.)/, "$1    $2").trim();

but does not work as I expected, because align from left to right.

Thanks a lot !

Advertisement

Answer

I guess you could use string.slice().

The regex /^(s*S+(s+)(?:S+s+){2}S+)s+(S+)s+(S+)s+(S+)/

Explained

 ^                             # BOL
 (                             # (1 start), Written back unchanged
      s* S+ 
      ( s+ )                       # (2), Minimum Column spacing
      (?: S+ s+ ){2}
      S+ 
 )                             # (1 end)
 s+ 
 ( S+ )                       # (3), Column 5 data
 s+ 
 ( S+ )                       # (4), Column 6 data
 s+ 
 ( S+ )                       # (5), Column 7 data

JS code

var Sary = [
"17/07/13     03:50:24     12.122N      88.214W     21     4.0     107 Km al suroeste de Boca del Padre Ramos",
"17/07/12     20:51:36     10.630N       6.634W     16     3.3     94 Km al noroeste de Tamarindo, Costa Rica",
"17/07/12     13:28:09     13.389N      87.986W     4     2.8     17 Km al noroeste de La Union, El Salvador",
"17/07/12     13:20:49     11.030N      85.177W     225     2.2     41 Km al sureste de Cárdenas",
"17/07/11     13:04:47     13.389N      87.981W     1     3.2     16 Km al noroeste de La Union, El Salvador",
"17/07/11     12:01:34      9.241N      83.983W     27     2.5     14 Km al oeste de Dominical, Costa Rica"]
;

for(var i=0, len=Sary.length; i < len; i++)
{
    var newstr = Sary[i].replace(/^(s*S+(s+)(?:S+s+){2}S+)s+(S+)s+(S+)s+(S+)/g,
    function(match, g1, g2, g3, g4, g5) {
      return g1 + 
      g2 + (String("   " + g3).slice(-3)) +
      g2 + (String("   " + g4).slice(-3)) +
      g2 + (String("   " + g5).slice(-3)) ;
    });
    console.log( newstr );
}

Output

17/07/13     03:50:24     12.122N      88.214W      21     4.0     107 Km al suroeste de Boca del Padre Ramos15:32:26.352
17/07/12     20:51:36     10.630N       6.634W      16     3.3      94 Km al noroeste de Tamarindo, Costa Rica15:32:26.353
17/07/12     13:28:09     13.389N      87.986W       4     2.8      17 Km al noroeste de La Union, El Salvador15:32:26.353
17/07/12     13:20:49     11.030N      85.177W     225     2.2      41 Km al sureste de Cárdenas15:32:26.353
17/07/11     13:04:47     13.389N      87.981W       1     3.2      16 Km al noroeste de La Union, El Salvador15:32:26.353
17/07/11     12:01:34      9.241N      83.983W      27     2.5      14 Km al oeste de Dominical, Costa Rica15:32:26.353
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement