First of all, you need to remove the quotes around your regex—if they're there, the argument won't be processed as a regex. JavaScript will see it as a string (because it is a string) and try to match it literally.
Now that that's taken care of, we can simplify your regex a bit:
arrayValues[index].replace(/[\s\S]*?<Section>/, "---");
[\s\S]
gets around JavaScript's lack of an s
flag (a handy option supported by most languages that enables .
to match newlines). \s
does match newlines (even without an s
flag specified), so the character class [\s\S]
tells the regex engine to match:
\s
- a whitespace character, which could be a newline
OR
\S
- a non-whitespace character
So you can think of [\s\S]
as matching .
(any character except a newline) or the literal \n
(a newline). See Javascript regex multiline flag doesn't work for more.
?
is used to make the initial [\s\S]*
match non-greedy, so the regex engine will stop once it hits the first occurrence of <Section>
.