For example, I have a string
“A – B – C asdas K – A,B,C”
Let the character be “-“
I want to save everything before the last occurrence of “-” so “A – B – C asdas K ” should be saved.
I have tried this:
str = str.split(":").pop();
How can I do this?
Advertisement
Answer
You can do something like this:
var str = "A - B - C asdas K - A,B,C"; console.log(str.substring(0, str.lastIndexOf("-")));
Firstly, you get the last index of the char that interest you: ‘-‘, then you are using substring which get 0-X string.