The delimiter is a colon :
and the format will always be a word bookended by colons. That pattern may appear more than once in the overall string. The word or characters bookended by colons are not predictable.
I need to remove the word between the colons, and the colons, and any remaining white space that was related to those colon-bookended words.
I’m close, but the regex needs work. I’m not good at regex. Here’s what I have so far:
const remove = (string) => string.replace(/ *:[^)]*: */g, '')
// Output should be 'Hello World'
const example1 = ':stuff: Hello World'
console.log(remove(example1))
// >>> "Hello World"
// Output should be 'Goodbye World'
const example2 = ':stuff: Goodbye World :stuff:'
console.log(remove(example2))
// >>> ""
// Output should be 'Hello Again'
const example3 = ':stuff::stuff:Hello Again'
console.log(remove(example3))
// >>> "Hello Again"
// Output should be 'Goodbye Again'
const example4 = ':stuff: :stuff: Goodbye Again'
console.log(remove(example4))
// >>> "Goodbye Again"
// Output should be ''
const example5 = ':stuff::stuff::stuff:'
console.log(remove(example5))
// >>> ""
It worked in 4 of the 5 examples, except for example2
where the regex matched and removed the value Goodbye World
because it was between two colons.
How can I adjust this?