2

I have the following input:

<script src="common/scripts/vendor.0.js"></script>
<script src="common/scripts/all.0.js"></script>
<script src="common/scripts/config.js"></script>

I'm trying to write a Grunt task to do a match/replace with JavaScript regex to something like this:

{
    match: /<(script.*?src=)"(.*?)"(.*?)>/g,
    replacement: '<$1"//<%= config.cdn.preview[0] %>/$2"$3>'
}

And I get this output:

<script src="//cdn.domain.com/common/scripts/vendor.0.js"></script>
<script src="//cdn.domain.com/common/scripts/all.0.js"></script>
<script src="//cdn.domain.com/common/scripts/config.js"></script>

So far so good... The only thing is... I don't want to do the match/replace the line with the config.js file, only the other two. I want the output to be this:

<script src="//cdn.domain.com/common/scripts/vendor.0.js"></script>
<script src="//cdn.domain.com/common/scripts/all.0.js"></script>
<script src="common/scripts/config.js"></script>

I've tried countless things I found here on SO and Google but can't seem to find a solution for this.

rfgamaral
  • 16,546
  • 57
  • 163
  • 275
  • It may be easiest to use [`grunt-regex-replace`](https://github.com/bomsy/grunt-regex-replace) to specify the directory (minus `config.js`) that you want to do the match/replace on. – Sam May 28 '14 at 17:51
  • http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 \*ducks\* – aebabis May 28 '14 at 17:52
  • What is the spec for the lines you want to replace? Just NO to config? Or YES to all 0.js? – zx81 May 28 '14 at 17:53
  • @Sam I don't see how that task would help. It seems to do exactly the same as the one I'm using. – rfgamaral May 28 '14 at 17:54
  • @zx81 NO to config. The number will change with the build number. – rfgamaral May 28 '14 at 17:54
  • You should be able to set a source like: `src: ['./scripts/**/*.js', '!./scripts/config.js']` – Sam May 28 '14 at 17:55
  • And config is only specified in environment.js? – zx81 May 28 '14 at 17:56
  • @Sam I'm not sure I understand that. The src attribute is the file(s) to read the contents from and then to the match/replace. I'm processing the index.html file, which contains my input with all those script tags. – rfgamaral May 28 '14 at 17:59
  • @zx81 I'm sorry, that was a typo, please refresh the question, I just updated it. – rfgamaral May 28 '14 at 18:00
  • OH! I get your question. Sorry, nevermind me. – Sam May 28 '14 at 18:00
  • @RicardoAmaral `I just updated it.` And I updated the answer. :) – zx81 May 28 '14 at 18:02
  • FY Added explanation. – zx81 May 28 '14 at 18:08

1 Answers1

2

Let's say you want to exclude the line with `config.js' This is what you can do:

Search:

<script src="common(?![\w/]+config\.js)

Replace:

<script src="//cdn.domain.common

See demo (look at the substitutions section at the bottom)

Your exact search and replacement criteria may be different, but I'm sure you'll be able to tweak that.

How does it work?

  1. <script src="common looks for scripts starting with common
  2. The (?![\w/]+config\.js) negative lookahead asserts that what immediately follows is not a number of word characters and slashes followed by config.js
zx81
  • 41,100
  • 9
  • 89
  • 105
  • 1
    @Sam The `.*?` would be a problem if you have two scripts on the same line, it will eat over the end of the first script. See this [demo](http://regex101.com/r/eB7yU5): the first two no longer match. – zx81 May 28 '14 at 18:06
  • Ugh I'm out of it today, all distracted by the 3D printer I just kickstarted haha..removing my comment to avoid confusion. – Sam May 28 '14 at 18:09
  • @RicardoAmaral What do you mean? See the substitutions on the demo: the output strings are what you wanted. Why do you want to "catch" something more? (Not understanding your question.) – zx81 May 28 '14 at 18:13
  • I'm not doing that on my original question, but say I also wanted to make sure that that regex would match the final ``? Maybe I shouldn't be doing this, but I was just wondering. – rfgamaral May 28 '14 at 18:18
  • 1
    @RicardoAmaral That's not a problem with an additional lookahead, either just before or just after the negative lookahead. But for your specs, I don't see the need to give the regex engine more work. – zx81 May 28 '14 at 18:29