KKK
KKK

Reputation: 1662

Remove Javascript comments RegEx

Hi I'm trying to remove all javascript comment (//) with in the HTML document. for example

<html>

<img src="http://example.com/img.jpg" />

<script>
//Some comments

gallery: {
      enabled: true,
      navigateByImgClick: true,
      preload: [0,1] // Will preload 0 - before current, and 1 after the current image
    },
</script>

</html>

Following is my regex code [^(http?s:)|ftp]\/\/(.*). This is working. But I want to make sure, Is there any way to improve this code. ?

Upvotes: 0

Views: 114

Answers (1)

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324620

Your regex says "match any character that isn't in fhpts?:(), followed by two literal slashes and anything to the ened of the line"

Normlly you'd want to do (?<!http)(?<!https)(?<!ftp)\/\/.*, however JavaScript doesn't support lookbehinds (much to everyone's disappointment) so consider doing this:

.replace(/\s\/\/.*/,"")

This will require comments to have a space before them (which they almost always do) - it's not perfect, but it's the best I can think of right now XD

Upvotes: 1

Related Questions