0

I am trying to strip out code between some tags. Its from a JavaScript plugin and it has multiple occurencies.

For example:

/*<ltIE8>*/                                ╗
if (!item.hasOwnProperty) return false;    ╣ this should match / go away
/*</ltIE8>*/                               ╝
return item instanceof object;             // this should not go away/match
...
/*<ltIE8>*/                                     ╗
if (!window.addEvenetListener) return false;    ╣ this should match / go away
/*</ltIE8>*/                                    ╝
return window.addEvent;

I would like to match/remove those two blocks.

Tried using lookaheads like \/\*<ltIE8>\*\/(?!=\/\*<\/ltIE8>\*\/)([\s\S]+) but it ends up matching from the first ocurrence to the last, and missing the ones in-between.

Example: https://regex101.com/r/iD6mL8/1

Any sugestions? (I will be doing these replacements using JavaScript/NodeJS).

4
  • 1
    How many is multiple? I would just do it by hand, I trust my hand more than I trust regex Commented Jan 2, 2015 at 16:10
  • 1
    If you're planning on doing this on the serverside, to ouput different things for different browsers, first of all, don't, and secondly, use a templating language, like Jade or EJS with a condition instead of searching the content with regex. Commented Jan 2, 2015 at 16:11
  • @adeneo I want to do it just once, not browser sniffing. I understand your point. Its about 40 occurencies. Commented Jan 2, 2015 at 16:13
  • @musefan Yes I could do it by hand, just would like to be effective and got into a problem I could not solve myself. Commented Jan 2, 2015 at 16:13

2 Answers 2

1
\/\*<ltIE8>\*\/([\s\S]+?)(?=\/\*<\/ltIE8>\*\/)

Try this.See demo.

https://www.regex101.com/r/fG5pZ8/18

Sign up to request clarification or add additional context in comments.

3 Comments

This doesn't seem to include the trailing comment, i.e. /*</ltIE8>*/
@musefan i am not sure OP wants that.Lets wait for his call
I dunno, the "this should match / go away" scope seems pretty clear to me. But to be fair, leaving in the comments isn't going to break anything (just will look silly and add to file size)
0

I suck at regex, but this seems to work:

(\/\*<ltIE8>\*\/)[\s\S]*?(\/\*<\/ltIE8>\*\/)

The key to this solution is the ?, it tells the regex to not be greedy which basically means it stops when it finds the next /*</ltIE8>*/ rather than going all the way to the very last one.

Here is a working example

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.