views:

73

answers:

3
+4  Q: 

quoting a string

Hi there. 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";
});

The 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?

+6  A: 

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.

Mark Byers
You should better split `<!` as that’s already a [token in SGML](http://www.is-thought.co.uk/book/sgml-4.htm#Fig4-4) (markup declaration open).
Gumbo
@Gumbo: Perhaps both... the Javascript code could already be inside an HTML comment.
Mark Byers
@Mark Byers: Then the same should be applied to `--` in the closing `-->`.
Gumbo
@Mark Byers: And `<!` is not a the “start of a comment declaration” but a *markup declaration open*. It’s also used in a doctype declaration (`<!DOCTYPE … >`), a CDATA declaration (`<!CDATA[ … ]]>`), an element declaration (`<!ELEMENT … >`), an attribute list declaration (`<!ATTLIST … >`), an entity declaration (`<!ENTITY … >`), etc.
Gumbo
+3  A: 

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.

Nick Craver
@nick-craver. It works in jquery 1.4.2 but I have 1.3.2 on my server. Anyway to get it to work for 1.3.2?
keith
@keith - Here's a 1.3.2 version: http://jsfiddle.net/nick_craver/Rzqdk/4/
Nick Craver
@nick-craver - thank you so much. Now I just need to figure out how to replace the xmlns:fb="http://www.facebook.com/2008/fbml" in the html tag so my page will validate!
keith
@keith - If you do a replace it's still not valid, the initial markup's still coming down, you can add a `.replace()` for that string if you must...but I don't think it'll help the validation piece any.
Nick Craver
A: 

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.

Reinis I.