0

How i can remove

src="../assets/default/js/jquery-1.4.2.min.js"

from this line

<script type="text/javascript" src="../assets/default/js/jquery-1.4.2.min.js">

using regex?

6 Answers6

2

You didn't specify a regular expression dialect, but this one should work in most:

src=".*?"
Carl Norum
  • 219,201
  • 40
  • 422
  • 469
  • i also think this was the solution but this returns something different. "> // \r\n \r\n –  Jun 07 '11 at 21:03
1

Though I need to preface this with the usual disclaimer about parsing HTML with regex, the following should work for input strings very similar to the example you gave:

/src=(["']).*?\1/

You didn't say what regex language you're using, but replace whatever matches the above pattern with an empty string.

The main advantage of this version is that it will accept both src="..." and src='...', but not src="...' or src='...". It also won't be thrown off by stuff like src="...foo='bar'...", although that's unlikely to come up.

Community
  • 1
  • 1
Justin Morgan - On strike
  • 30,035
  • 12
  • 80
  • 104
1

If you're doing this to prevent linking to external scripts, don't. It won't work.

  • The inline script can easily inject a new script tag, or otherwise load an external script.
  • No sane regexp will handle all the variations of character encodings, multiple src="…" attributes (legal according to the spec? I doubt it; does it work? I bet), browser parsing bugs, etc.
  • No regexp can handle things like matching quotes correctly.

You'll get to look forward to:

<!-- this is even in-spec, except for the made-up attributes fakeout and oops.
     a trivial out-of-spec bit of fun: what happens if we drop the final quote?
     I bet browsers would still figure it out. -->
<script type="text/javascript" fakeout="src=" oops="" src
                                                      ="http://example.com/oops.js">
    document.write("<script sr" + 'c="http://example.com/oops.js"></script>");
</script>

Now, if instead you're just trying to find all the externally-referenced scripts in a site you maintain (e.g., to see if a random script is still used), then many of the other answers will do.

derobert
  • 49,731
  • 15
  • 94
  • 124
  • interesting to know this, but im not doing this. –  Jun 07 '11 at 21:11
  • @Vanilla: It would help to know what you *are* doing. Also keep in mind that SO questions/answers are intended to help people who stumble across them in the future, so I think its important to explain this caveat. – derobert Jun 07 '11 at 21:13
  • 1
    I agree. A regex will be perfectly fine for scraping a bunch of your own files, but should never be used to sanitize against script injection. – Justin Morgan - On strike Jun 07 '11 at 21:15
  • Well, it can be troublesome for scraping your own files too, for example the missing closing quote can easily be a typo, and will lead the regexp to produce some quite interesting results. – derobert Jun 07 '11 at 21:16
0

Use src=".*?" in replace and replace with empty string.

manojlds
  • 290,304
  • 63
  • 469
  • 417
-1

Here's my stab at it:

s/\s*src="[^"]*"//

That one also removes any whitespace before the 'src' (via the '\s*').

Aron
  • 1,552
  • 1
  • 13
  • 34
-1

Solution using sed

sed 's/\(^.*\) src.*/\1>/'
<script type="text/javascript">
Fredrik Pihl
  • 44,604
  • 7
  • 83
  • 130