1

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?

0

3 Answers 3

4

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);
Sign up to request clarification or add additional context in comments.

1 Comment

Hey I am actually looking for a regex type answer. But cool solution anyway!
3

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]]);
  }
}

1 Comment

Thanks! This is a great regex solution.
0

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];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.