I have a regex that matches the words with double curly brackets:
let str = 'This is a {{test}} string with {{multiple}} matches.';
let regex = /\{\{(.+?)\}\}/g;
let matches = str.match(regex);
console.log(matches); // ['{{test}}', '{{multiple}}']
Now I want to split str into chunks by matches array:
let regex2 = new RegExp(`\\b${matches.join('|')}\\b`, 'gi');
let substrings = str.split(regex2);
console.log(substrings);
But the output of substrings is "This is a {{test}} string with {{multiple}} matches.".
What I was expected is: ["This is a ", "{{test}}", " string with ", "{{multiple}}", "matches."]
But I know my regex is correct because if I do it without using those double curly brackets, it works. but I must need to use them to get what I want.
So why regex doesn't split them as I was expecting? how can I change the code so that it will work as expected?
let regex2 = new RegExp(`(${matches.join('|')})`, 'i');should suffice.