Reputation: 1102
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
Upvotes: 1
Views: 523
Reputation: 2405
You can solve this with a cheeky .replaceAll
and simple strings instead of regex 🚀
const delimiters = ['Q) ', 'A) ', 'B) ', 'C) ', 'E) '];
const start = 'Some textC) Some textE) Some text';
let output = start;
for (const str of delimiters) {
output = output.replaceAll(str, "\n"+str);
}
console.log(output);
EDIT: Looking at this question some time later, I thought I might add a way to solve this with regex. Assuming we know the targets and are not given an array, we can use capture groups to choose what we replace.
Let us walk through the process of making the regular expression our code will use.
an empty regular expression with global tag to target every occurance
/ /g
targets every capital Q
/Q/g
includes the right parenthesis, needs to be escaped because ) is a special character in regex
/Q\)/g
wrapping part of our regex in ( ) will capture that group for use
/(Q\))/g
now we want to capture more than just Q, so we will use a set or range where Q is, in example [QA]
would target either Q
or A
. So replacing Q
with [QABCE]
will target all of those characters.
/([QABCE]\)/g
The second argument of our .replace
method on a string, is what we are replacing the target with. This is where the capture group is useful. We can use $1
to insert our first (and only) capture group into what we are replacing. So to replace with a newline before the capture group would look like this, \n$1
All put together it looks like this:
const start = 'Some textC) Some textE) Some text';
const output = start.replace(/([QABCE]\))/g, "\n$1");
console.log(output);
However, this assumes that you can organize your delimiters into the regex manually instead of being provided an array of delimiters. You could use the array to construct a regular expression as well, however you would need to use the regex constructor, new RegExp()
and escape special characters / extract the meaningful parts from your array. Which the other answer posted does a good job of providing an example of this.
Upvotes: 3
Reputation: 13641
The first answer posted gives the simplest and best solution I believe, but here is an example of doing it with a regular expression.
const delimiters = ['Q) ', 'A) ', 'B) ', 'C) ', 'E) ']
const str = 'Some textC) Some textE) Some text';
const pattern = `(?=${delimiters.join('|').replaceAll(')', '\\)')})`;
console.log(`Pattern: "${pattern}"`);
console.log(str.replace(new RegExp(pattern, 'g'), '\n'));
Note that care should be taken to escape any characters in the delimiters that have special meaning in a regular expression.
Above, I just use .replaceAll(')', '\\)')
to escape the )
after joining the delimiters with the regex alternation character |
in a positive lookahead (?=alternative1|alternative2|etc)
before passing it to the RegExp
constructor.
Note also that this method of joining with |
would not result in a reliably working regular expression if a delimiter itself contained the |
character.
Upvotes: 1