I'm trying to write a regular expression but I have encountered difficulties.
My goal is to split the contents within 2 delimiters with some conditions.
var str = "foo -word- a b -word2- a b-c -word3- h- b-";
var result = [
"foo", "word", "a b",
"word2", "a b-c",
"word3", "h- b-"
var result = str.split(\-(.*?)-\);
The problem is that you are using
.* when you could be specifying a range or class of characters. Try this:
var result = str.split(-(\S+)-);
\S is any non-whitespace character, and this is looking for any one or more of them.