-1

I'm trying to use grunt-string-replace to replace the contents of a file between 2 HTML comments. What I have come up with works fine over a single line but for the life of me, I cannot get it to match where there are multiple lines.

<!-- START COMMENT -->This matches<!-- END COMMENT -->

<!-- START COMMENT -->
this
doesn't
match
<!-- END COMMENT -->

My regex I have currently is

/<!-- START COMMENT -->[\s\S]*?<!-- END COMMENT -->/g

I understood that replacing the .*? (which worked for a single line) with [\s\S]*? would allow a match over multiple lines but I can't seem to get it to work.

Anyone have any ideas?

Update: As commenters below mentioned, my regex was fine, it was how I was implementing it with grunt

//This didn't work
.....
options:{
    replacements: [
        {
            pattern: "/<!-- START COMMENT -->[\s\S]*?<!-- END COMMENT -->/g",
            replacement: '<script src="'+minJSDestination+'"></script>'
        }
    ]
}
.....


//This does work
.....
options:{
    replacements: [
        {
            pattern: /<!-- START COMMENT -->([\s\S]*?)<!-- END COMMENT -->/ig,
            replacement: '<script src="'+minJSDestination+'"></script>'
        }
    ]
}
.....
Fraser
  • 14,036
  • 22
  • 73
  • 118

1 Answers1

1

In this line:

pattern: "/<!-- START COMMENT -->[\s\S]*?<!-- END COMMENT -->/g",

You have quotes around the RegEx. Remove them and it should work fine:

pattern: /<!-- START COMMENT -->[\s\S]*?<!-- END COMMENT -->/g,
Downgoat
  • 13,771
  • 5
  • 46
  • 69
  • Thanks for re-pointing out what I had already pointed out in my edit from which you got the code sample :) – Fraser Jun 18 '15 at 16:04