0

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

4
  • 1
    use regex for this Commented Aug 24, 2017 at 8:00
  • 1
    What exactly is the logic? Anything that matches the regex \w+,\w+…? (hint hint) Commented Aug 24, 2017 at 8:01
  • let me try this with regex Commented Aug 24, 2017 at 8:01
  • What is this "value"? What is it supposed to represent? Why do you deal with multidimensional data in a string seperated by ; and ,? Or are you trying to parse it? Commented Aug 24, 2017 at 8:22

2 Answers 2

5

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'));

Sign up to request clarification or add additional context in comments.

3 Comments

This is working for small values but if not long values. I am trying this. "bayern munich germany;wayne rooney, manchester united; messi plays barcelona"
what is your expected output? this has to do with the spaces in between the words
Ya In this case my output should be wayne rooney, manchester united
1

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

2 Comments

you mean `var newVal = V.match(\w+(?=,))
V.match(/\w+(?=,)/g)

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.