In C#, Ruby, and many other languages you can denote a string as to not need escaping. In C# it’s like this
string s = @"\whatever\this\is";
The results are when printed:
\whatever\this\is
Is this supported in any form in JavaScript?
Short answer: No
Long answer: Noooooooooooooooooooooooooo
\useless or ${what is this} inside one of them.String.raw`\whatever\this\is`; . see hereI 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 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/
Literal strings are available through the use of ES6 language features. Node.js v4.x now supports these and around 90% of the other ES6 additions as well.
Template literals (Template strings)
In JavaScript, string literals are known as template strings. And the syntax is pretty straightforward.
String.raw() method (which is part of Template literals) is the closest you could get to template strings.String.raw`this is a \useless feature`: it results in “Uncaught SyntaxError: Invalid Unicode escape sequence”. Compare with say r'this is a \useless feature' in Python. So template strings (even with String.raw) interpret not only ${ but also \u (and other) escape sequences. The question's \whatever\this\is happens to work with String.raw, but that's only because none of the three words happened to start with u or x for example. :-)`\whatever\this\is` the result is "whatever hisis" (all backslashes are gone and the t has been turned into a tab). Another nit: JavaScript has always had the concept of "string literals" (meaning strings that are enclosed by quotes in code), but they are not verbatim string literals (or "literal strings" as the heading says).console.log(String.raw`\whatever\this\is\a\useless\xylophone`) will work now (starting sometime after this proposal), so now the only thing that a String.raw literal cannot contain (if you don't want it to be treated as special) is ${This will work as long as you don't throw a \x into the string!
var str = String.raw`\whatever\this\is`;
console.log(str);
\x isn't the only thing: you can't have a \u in the string either, for instance.I've got one solution to this ;)
function literalString(regex) {
return ('' + regex).slice(1, -1);
};
O.innerHTML = literalString(/\whatever\this\is/);
<pre id=O>
You basically convert a regex to a string and remove the first and last characters.
[a- or *{2,3} or ** by surrounding it with / on both sides.) This is even more onerous than simply needing to double backslashes.
"", and single-quote strings in Ruby still special-case\'and\\. as far as I understand, in a language where programs are expressed as text, any string literal needs some way to indicate where it ends- either with a terminator, or specifying length up-front (there are other, less-efficient/usable ways). if there's a terminator, and you want to be able to put the terminator in the string literal, then you'll need escaping. some languages allow you to choose a custom delimiter that won't occur in the literal. what exactly are you looking for?