views:

100

answers:

4

In c# and ruby and many other languages you can denote a string as to not need escaping.
in c# its like this

string s = @"\whatever\this\is";

the results are when printed

\whatever\this\is

my question is, is this supported in any form in javascript?

+12  A: 

Short answer: No

Long answer: Noooooooooooooooooooooooooo

Peter Bailey
The long answer is actually 'Noooooooooooooooooooooooooo'.
Skilldrick
@Skilldrick indeed it is :D
Peter Bailey
A: 

No, but you can use escape sequences to get the same effect. Check out the following link. It has a list of escape sequences for Javascript:

http://www.c-point.com/javascript_tutorial/special_characters.htm

According to the link your example would be:

var s = '\whatever\this\is';

It might help to undestand if there is a reason you can;t use the escape sequences to achieve your goal.

JookyDFW
A: 

I don't know what you're getting at, but one way to get around the problem of escaping (etc) is use a trick that John Resig seems to like a lot. You include <script> blocks in a page, but give them a "type" like "text/plain" to make sure that the browser doesn't hand them over to Javascript. Then use use the text of the script block for whatever you like.

<script id='a_string' type='text/plain'>
  Here is some stuff.
  There might be some \escape sequences in it.
</script>

Then you can grab that with $('#a_string').text() (or with getElementById if you're not using jQuery or something like that).

edit: Here's John Resig's explanation about why dropping stuff into script blocks like that is a good idea:

Quick tip: Embedding scripts in your page that have a unknown content-type (such is the case here - the browser doesn't know how to execute a text/html script) are simply ignored by the browser - and by search engines and screenreaders. It's a perfect cloaking device for sneaking templates into your page. I like to use this technique for quick-and-dirty cases where I just need a little template or two on the page and want something light and fast.

Taken from this page: http://ejohn.org/blog/javascript-micro-templating/

Pointy
What is the advantage of this over using some other random DOM element hidden from view with CSS? It seems like it breaks the semantics of the script element.
Syntactic
I don't know, frankly; I've been trying to figure that out. I first came across it in examples for the new jQuery "template" stuff. I agree that it seems squirrely, and especially so when you consider what jQuery does with script blocks when you dynamically insert content via "html()". Still, it's a thing that people do; I'm not sure what the OP is trying to discover or achieve.
Pointy
@Syntactic I've updated the answer with a snippet from Resig's blog
Pointy
Hadn't thought of the search engine angle. Interesting.
Syntactic
A: 

Just escape the escapes

var myCrazyString = "\\yes\\we\\have\\no\\bananas"
plodder