shanky singh
shanky singh

Reputation: 1141

How to remove character from fixed String

I have a value like

V = "Val1,Val2;Val3"

In this case If I need to separate "Val1,Val2" from V I am using this

var newVal = V.substr(0, V.indexOf(";"));

But this case is faling for some value like this

V = "Val4;Val1,Val2;Val3"

Any idea how to take only the value which having "," and remove all other character which is separated by ";"

Upvotes: 0

Views: 72

Answers (2)

Ashvin777
Ashvin777

Reputation: 1482

Use \w+(?=,) regex to get all those values

Upvotes: 1

marvel308
marvel308

Reputation: 10458

use the regex

\w+,\w+ 

to solve this

function match(str){
    return str.match(/\w+,\w+/g);
}

console.log(match('a;b,c;d'));
console.log(match('a,b;c'));

Upvotes: 5

Related Questions