var str = "in this sentence, I WANT TO GET ONLY THIS PART - not the other parts"
Assume I want to get the part which is starts with , and ends with -
I always use split in order to cut or get the sub-sentence. Because I don't know how to write a regex.
For this example I wrote as;
str.split(",")[1].split("-")[0]
" I WANT TO GET ONLY THIS PART "
Is it efficient to use split? Or how can I write a regex which will take the same part. Which one should I choose? regex or split? What do you suggest and why?
edit: Thank you for giving me the exact regex, but I also want to know which way is better? Using split is also a solution? Or should I learn how to write a regex?
.split()with a regex:str.split(/[,-]/)[1]. Orstr.split(/(?:, *)|(?: *-)/)[1]to avoid the leading and trailing spaces. (I'm not saying you should, but you could...)