I have this string that I split whenever it finds two colons.
"for exam:ple thi:s string turns into" ["for exam","pls thi", "s string turns into"]
I do this with:
text.split(/(:.*?:)/g)
Afterward I interpret a string as "inside" the delimiter if it contains the characters I expect and none of those I don't.
How do I make it also extract another delimiter like $$...$$ as well as the original :...: but also include the delimiter somewhere so I can use different logic for it?
For context I'm parsing text into different react components.:...: includes addresses to embed a certain component, while $$...$$ includes MathJax.
UPDATED
You can try this regex with particular checks for :...: and $$...$$ patterns
const text = "for exam:ple thi:s string $$turns$$ into"
const partialStrings = text.split(/(:.*?:|\$\$.*?\$\$)/g)
for (const partialString of partialStrings) {
if (partialString.startsWith(":")) {
//TODO: Do your logic with `:...:` pattern
console.log(partialString + " contains ':...:'")
continue
}
if (partialString.startsWith("$$")) {
//TODO: Do your logic with `$$...$$` pattern
console.log(partialString + " contains '$$...$$'")
continue
}
//no patterns
console.log(partialString + " has no patterns")
}
OLD ANSWER
You can simply split your string with :
const data = "for exam:ple thi:s string turns into"
console.log(data.split(":"))
If you want to have similar behavior for $$ and a reusable function, you can declare a function called splitBy
const splitBy = (data, delimiter) => {
return data.split(delimiter)
}
const data = "for exam$$ple thi$$s string turns into"
console.log(splitBy(data, "$$"))