Home > Back-end >  JavaScript String insert newline before every occurrence of delimiter from list
JavaScript String insert newline before every occurrence of delimiter from list

Time:04-10

In JavaScript, how do I insert a newline character before every occurrence of a delimiter from a list of delimiters?

Example:

const delimiters = ['Q) ', 'A) ', 'B) ', 'C) ', 'E) ']
const str = 'Some textC) Some textE) Some text'

I want to create the String:

Some text
C) Some text
E) Some text

CodePudding user response:

You can solve this with a cheeky .replaceAll and simple strings instead of regex

  • Related