3

So I have 1000 lines of javascript. I need to turn it into a Java String so that I can output (via System.out.println or whatever).

I'm looking for an online tool to escape all the quotes... something geared toward my specific need would be nice as I don't want other special characters changed. Lines like:

var rgx = /(\d+)(\d{3})/;

need to stay intact.

The situation mandates the JavaScript be put into a String so please no workarounds.

1
  • 1
    +1 to counter cowardly drive-by downvoter. It's not an unclear or unhelpful question. It may not be a smart thing to do, but the question is clear enough. Commented Jan 13, 2009 at 20:27

4 Answers 4

5

Here's a link which features Crockford's implementation of the quote() function. Use it to build your own JavaScript converter.

Edit: I also slightly modified the function to output an ascii-safe string by default.

Edit2: Just a suggestion: It might be smarter to keep the JavaScript in an external file and read it at runtime instead of hardcoding it...

Edit3: And here's a fully-featured solution - just copy to a .html file and replace the dummy script:

<script src="quote.js"></script>
<script>
// this is the JavaScript to be converted:
var foo = 'bar';
var spam = 'eggs';

function fancyFunction() {
    return 'cool';
}
</script>
<pre><script>
document.writeln(quote(
    document.getElementsByTagName('script')[1].firstChild.nodeValue, true));
</script></pre>
Sign up to request clarification or add additional context in comments.

4 Comments

A better version of this script which also escapes quotes: jsbin.com/ujumus/11
The two links in the answer to not work, but the link of @Yahor still works to see a quote example in js. Nevertheless it is worthwhile looking into Mr Crockford's.
@Yahor in the js bin, I cannot see in the html where the js quote file is included. And I do not know what the other functions besides quote do.
@Timo a simplified version of the same script jsbin.com/qifisaromi
1

You can compress the file using one of the available tools to achieve this effect:

YUI Compressor Online

Dean Edward's Packer

Douglas Crockford's JSMIN

3 Comments

I don't see how this is relevant, the question is about escaping quotes in a String not making 1000 lines of JavaScript smaller.
They make 1000 lines of JavaScript one line of JavaScript.
@Diodeus: But that's not what Paul wants to do - he's looking for a function which produces appropriate escape sequences for unsafe characters used in scripts...
0

You can use the jsmin tool to compress the Javascript to a single line (hopefully), but it doesn't escape the quotes. This can be done with search/replace in an editor or the server side scripting language used.

1 Comment

the number of lines is relevant, you can have new line characters in a String.
0

So everything I tried ended up breaking the javascript. I finally got it to work by doing the following:

Using Notepad++:

  1. Hit Shift + Tab a bunch of times to unindent every line

  2. Do View -> Show End Of Line

  3. Highlight the LF char and do a Replace All to replace with empty string
  4. Repeat for the CR char

  5. Highlight a " (quote character) and do a Replace All with \" (escaped quote)... just typing the quote character into the Replace prompt only grabbed some of the quotes for some reason.

  6. Now You have 1 enormously long line... I ended up having to break the 1 string apart into about 2000 character long lines.... The crazy long line was killing IE and/or breaking the Java String limit.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.