2

How do I split a string with multiple separators in JavaScript? I'm trying to split on both commas and : colon but, js's split function only supports one separator.

Example :

materialA:125,materialB:150,materialC:175

I want to split both these values into array like

materiaA,materialB,materialC

and second

125,150,175

Or anybody can give me idea how could I multiply these numbers with a constant to get like

materialA:1250, materialB:1500,materialC:1750. 
Robin
  • 9,415
  • 3
  • 34
  • 45
user3250071
  • 25
  • 1
  • 9

5 Answers5

4

You can split with more than one seperator if you're using regex:

.split(/:|,/)

This would give

["materialA", "125", "materialB", "150", "materialC", "175"] 
XiozZe
  • 464
  • 2
  • 7
  • Neat, I had no idea you could pass in a regex (and didn't know about the | char either). I've learned something new. – mitim May 05 '14 at 10:22
1

You could split the string by comma first, then loop through the resulting array. In that array, each entry would be something like "materialA:125". From there, you can split by the colon and append each part to its own list to work with or if you prefer, just multiply the second half (cast to int first) and rejoin it in to your original string.

Even though someone gave a much better answer, here's a bit of code that does what I mentioned above (since you asked)

var inputString = "materialA:125,materialB:150,materialC:175";

var mats = new Array();
var numbers = new Array();

var temp;
var elements = inputString.split(",");
for(var element in elements){
    temp = elements[element].split(":");
    mats.push(temp[0]);
    numbers.push(parseInt(temp[1]));
}

console.log(mats); // prints ["materialA", "materialB", "materialC"] 
console.log(numbers); // prints [125, 150, 175] 
mitim
  • 3,169
  • 5
  • 21
  • 25
1

Changing the approach completely, if all you want to do is multiply all the numbers in your string by a fixed coefficient, you can use string.replace:

var string = "materialA:125,materialB:150,materialC:175";
var coef = 10;

var result = string.replace(/\d+/g, function(match){
    return parseInt(match)*coef;
    });

Then print(result) outputs the string

materialA:1250,materialB:1500,materialC:1750

\d is a shortcut for [0-9].

Robin
  • 9,415
  • 3
  • 34
  • 45
1

Example using @mitim's method:

var str = 'materialA:125,materialB:150,materialC:175',
    multiplier = 2;

str = str.split(',').map(function (elem) {
    var parts = elem.split(':');
    parts[1] *= multiplier;
    return parts.join(':');
}).join(',');

This will give you:

materialA:250,materialB:300,materialC:350
Renaat De Muynck
  • 3,267
  • 1
  • 22
  • 18
0

You could simply use following Regex:

/[:,]/

And following string method:

mystring = 'materialA:125,materialB:150,materialC:175';
result = mystring.split(/[:,]/);

Here is a Fiddle.

Marvin
  • 539
  • 5
  • 17
  • I apreciate your answer it worked fine and give me this result materialA:50,materialB:50,materialC:50 but how could I multiply these numbers with constant....like multiply by 10 gives me materialA:500,materialB:500,materialC:500 – user3250071 May 05 '14 at 10:18