Souvik Ray
Souvik Ray

Reputation: 3018

How to use regex to parse a string in a certain format in Javascript?

I have some strings like below

ab.ab.c,ab.d,bc.e,mt

bc.e,nmt

ef.1,mt

What I want is to use regex to parse these strings in certain way and get the result

For example

ab.ab.c,ab.d,bc.e,mt

val1 = ab  (take the value prior to first dot)
val2 = ab.c,ab.d,bc.e (take the value after first dot but before the last comma)
val3 = mt (take value after the last comma)

Similarly

bc.e,nmt

val1 = bc (take the value prior to first dot)
val2 = e (take the value after first dot but before the last comma)
val3 = nmt (take value after the last comma)

ef.1,mt

val1 = ef
val2 = 1
val3 = mt

Now my approach is pretty longer and inefficient. This is what I do

let val3 = someString.split(',').slice(-1)[0]
let remaining_string = someString.replace("," + val3, "")

let val1 = remaining_string.slice(0, remaining_string.indexOf('.'))    

let val2 = null
if(remaining_string.split(",").length > 1) {
    val2 = remaining_string.replace("."+val1, "")
 }
 else {
    val2 = remaining_string.split(".")[1]
 }

Is there any one liner or a cleaner solution to get val1, val2 and val3 quickly?

Upvotes: 1

Views: 157

Answers (3)

The Ashen Wolf
The Ashen Wolf

Reputation: 76

I am not very experienced with RegEx, so there might be a better solution, but this seems to work for me :)

val1 = string.match(/^[^\.]*/)[0];
val2 = string.match(/(?:\.)+(.*)(?:,)/)[1];
val3 = string.match(/[^,]*?$/)[0];

Upvotes: 0

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626754

You may use a generic regex to match and capture 1) any 1+ word chars at the start of a string into Group 1, 2) any chars after the first non-word char into Group 2 up to the last occurrence of a non-word char 3) and then any 1+ word chars at the end of the string.

 /^(\w+)\W(.*)\W(\w+)$/

See the regex demo

NOTE: If the separators are fixed chars use /^(\w+)\.(.*),(\w+)$/.

JS demo:

var rx = /^(\w+)\W(.*)\W(\w+)$/;
var ss = ["ab.ab.c,ab.d,bc.e,mt", "bc.e,nmt", "ef.1,mt"];
for (var i=0; i<ss.length; i++) {
  var m = ss[i].match(rx);
  if (m) {
     console.log([m[1], m[2], m[3]]);
  }
}

Upvotes: 3

Udith Gunaratna
Udith Gunaratna

Reputation: 2111

First, find the indexes of first dot and last comma.

var idxPeriod = someString.indexOf(".");
var idxComma = someString.lastIndexOf(",");

Then use substring() method to extract the necessary part.

var val1 = someString.substring(0, idxPeriod);
var val2 = someString.substring(idxPeriod + 1, idxComma);
var val3 = someString.substring(idxComma + 1);

Upvotes: 4

Related Questions