1

I'm working on a grunt file for work. We work locally with our files being in html and when we implement them into our CMS they need to be converted to aspx.

I'm trying to replace all instances of <script scr=""></script> with <imod:ScriptPlaceHolder id="#nameOfFile" runat="server" type="FileInclude" url="#pathToFile"></imod:ScriptPlaceHolder>

I'm using "grunt-replace": "^0.8.0." This task is registered under default and build. I've tried using a regex to match the name, and I've been looking at the documentation for far too long. Here is what my grunt file with replace looks like:

replace: {
      dist: {
        options: {
          patterns: [
            {
              match: '/script src="/g',
              replacement: function() {
                'asp:PlaceHolder runat="server" url="';
              }
            }
          ]
        },
        files: [
          {expand: true, flatten: true, src: ['<%= config.dist %>/index.html'], dest: 'dist/'}
        ]
      }
    },
zach57
  • 442
  • 4
  • 20

2 Answers2

0

you don't mention the result you're currently getting so i assume you're seeing the script tag simply truncated or "undefined" prepended to the remaining bit of script tag.

I find http://www.regexr.com/ useful for testing regexes.

try changing your match to - '/<script\ssrc=".+</script>/g'

also, your replacement function needs to return a value. obviously if you're replacing with a static string then you can use a string for the argument instead.

Matthew Kime
  • 754
  • 1
  • 6
  • 15
0

With some help from Matthew Kime, I was able to get this working. In my regex I had to search for 2 periods because I'm using grunt-rev to version my files. I also used the file name stripped of it's extension to name the files dynamically.

Here is the conclusion I was able to arrive at.

// replace script tags to imod placeholders
    replace: {
      dist: {
        options: {
          patterns: [
            {
              match: /<script src=\"(.*\/)(.+?)\.(.+?)\.js\"><\/script>/g,
              replacement: '<imod:ScriptPlaceHolder id="$2$3" type="FileInclude" url="$1$2.$3.js" runat="server"></imod:ScriptPlaceHolder>'
            }
          ]
        },
        files: [
          {expand: true, flatten: true, src: ['<%= config.dist %>/index.html'], dest: 'dist/'}
        ]
      }
    },
zach57
  • 442
  • 4
  • 20