开发者

quoting a string

I found a bit of code on the web that I would like to use.

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

Th开发者_StackOverflowe problem is the browser thinks

<!--

is a real comment. How would I quote it in a way to tell the browser look for that string and it is not a real comment?


Escaping one of the symbols won't change the regular expression. You can use a backslash to prevent the browser from interpreting the -- as the start or end of an HTML comment:

/<!-\- FBML /g

Having said that, I don't know of any modern browser that would misinterpret a piece of Javascript as a comment if the Javascript is correctly enclosed in a <script> tag.


I think this is what you're after overall:

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

You can give it a try here, .html isn't a property of a jQuery object you can modify, you can however pass a function to .html() and perform the .replace() on each occurrence.


Instead of escaping the regex as the other answers suggest, I would just put the code in an external file if I were you. That way it could also be cached, making it a little bit more efficient, and there would be more separation between behavior (scripts) and structure (markup), making your project more manageable.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜