Replace and save the result in a new line, for example:
var str = ` Number One: Get this Number Two: And this`; var output = str.replace(/Number (?:One|Two): (.*)/g, "$1"); console.log(output);
which outputs:
Get this And this
If you need an array of matches, as you requested, you can try the following:
var getMatch = function(string, split, regex) { var match = string.replace(regex, "$1" + split); match = match.split(split); match = match.reverse(); match.push(string); match = match.reverse(); match.pop(); return match; } var str = ` Number One: Get this Number Two: And this`; var regex = /Number (?:One|Two): (.*)/g; var match = getMatch(str, "#!SPLIT!#", regex); console.log(match);
which displays the array as desired:
[ ' Number One: Get this\n Number Two: And this', ' Get this', '\n And this' ]
If split (here #!SPLIT!# ) Should be a unique string to separate matches. Please note that this only works for specific groups. For multiple groups, add a variable indicating the number of groups, and add a loop construction for "$1 $2 $3 $4 ..." + split .
source share