Quoting string

I found some code on the Internet that I would like to use.

$(document).ready(function() { $(".fbreplace").html.replace(/<!-- FBML /g, ""); $(".fbreplace").html.replace(/ -->/g, ""); $(".fbreplace").style.display = "block"; }); 

The problem is what the browser thinks

 <!-- 

- a real comment. How do I quote this to tell the browser this line, and this is not a real comment?

+4
source share
3 answers

Exiting one of the characters will not change the regular expression. You can use a backslash to prevent the browser from interpreting -- as the beginning or end of an HTML comment:

 /<!-\- FBML /g 

Having said that, I do not know of a single modern browser that would incorrectly interpret part of Javascript as a comment if Javascript is correctly enclosed in the <script> .

+6
source

I think this is what you after all:

 $(function() { $(".fbreplace").html(function(i, html) { return html.replace(/<!-\- FBML | -->/g, ""); }).show(); });​ 

Here you can try here , .html not a property of the jQuery object that you can change, however you can pass the .html() function and execute .replace() in each case.

+3
source

Instead of running away from regex as other answers suggest, I would just put the code in an external file if I were you. Thus, it can also be cached, which makes it a little more efficient, and there will be more separation of behavior (scripts) and structure (markup), which will make your project more manageable.

0
source

All Articles