I tried to use match
function, but I've got the array( ["$#word1#$", "$#word2#$", "$#word3#$"])
So, for your case I used function replace
function. Here is code:
const subStrings = [];
sentence.replace(/\$\#(.+?)\#\$/g, (str, word) => subStrings.push(word));
console.log(subStrings);
Output subStrings
: ['word1','word2','word3']
Additional information:
If you want to use exec
function you should take a look at this example:
var regex1 = RegExp('foo*','g');
var str1 = 'table football, foosball';
var array1;
while ((array1 = regex1.exec(str1)) !== null) {
console.log(`Found ${array1[0]}. Next starts at ${regex1.lastIndex}.`);
// expected output: "Found foo. Next starts at 9."
// expected output: "Found foo. Next starts at 19."
}
More information about replace
function:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace
More information about exec
function: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec