I remember seeing a clever solution a while ago that used multi-line comments in a function:
(function () {
/*
"This is an example of a multi-line string. It's really just a mult-line
comment, and it's wrapped in quote marks. You might also notice the
apostrophe's ;-)";
*/
});
Note: that last apostrophe is intentionally incorrect ;-P
The trick is to call the function's toString()
method and parse out the multi-line comment using a regular expression. Clever, but much like Pointy's suggestion, a bit of an abomination.
I didn't actually think the question to be looking for a seriously viable method for production uses -- my own fault for jumping to conclusions -- I'm not really sure why you wouldn't just escape the relevant string literal delimiters. As Tim Down pointed out in the comments below, ECMAScript 3rd edition defines toString() for functions as being implementation dependant.
For funsies, I decided to check out browser compatibility and this method is feasible in IE, Opera, Safari and Chrome but not Firefox, which does not include comments in the returned string. http://jsfiddle.net/2yvXG/