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. 
1
  • 1
    you should be able to figure out your answer by the answer provided in that question. Commented May 5, 2014 at 10:04

5 Answers 5

4

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

.split(/:|,/)

This would give

["materialA", "125", "materialB", "150", "materialC", "175"] 
Sign up to request clarification or add additional context in comments.

1 Comment

Neat, I had no idea you could pass in a regex (and didn't know about the | char either). I've learned something new.
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].

1 Comment

Thanks Robin its working fine. I exactly want this result. Thanks a lot..
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

1 Comment

a nice and more compact version then mine :)
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] 

1 Comment

Yes mitim but could you give me some example to explain little more as I'm new to javascript.
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.

1 Comment

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.