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).
所以我有1000行的JavaScript。我需要把它变成一个Java String,以便我可以输出(通过System.out.println或其他)。
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.
这种情况要求将JavaScript放入String中,所以请不要解决。
4 个解决方案
#1
Here's a link which features Crockford's implementation of the quote()
function. Use it to build your own JavaScript converter.
这是一个以Crockford实现quote()函数为特色的链接。用它来构建自己的JavaScript转换器。
Edit: I also slightly modified the function to output an ascii-safe string by default.
编辑:我还稍微修改了函数,默认输出ascii-safe字符串。
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...
Edit2:只是一个建议:将JavaScript保存在外部文件中并在运行时读取而不是硬编码可能会更聪明......
Edit3: And here's a fully-featured solution - just copy to a .html
file and replace the dummy script:
Edit3:这是一个功能齐全的解决方案 - 只需复制到.html文件并替换虚拟脚本:
<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>
#2
You can compress the file using one of the available tools to achieve this effect:
您可以使用其中一个可用工具压缩文件以实现此效果:
YUI压缩机在线
迪安爱德华的包装工
Douglas Crockford的JSMIN
#3
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.
您可以使用jsmin工具将Javascript压缩为单行(希望如此),但它不会转义引号。这可以通过编辑器中的搜索/替换或使用的服务器端脚本语言来完成。
#4
So everything I tried ended up breaking the javascript. I finally got it to work by doing the following:
所以我尝试的一切最终打破了javascript。我终于通过以下方式让它工作:
Using Notepad++:
-
Hit Shift + Tab a bunch of times to unindent every line
点击Shift + Tab多次以取消每一行
-
Do View -> Show End Of Line
查看 - >显示行尾
- Highlight the LF char and do a Replace All to replace with empty string
-
Repeat for the CR char
重复CR char
-
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.
突出显示“(引用字符)并执行全部替换”(转义引号)...只需在“替换”提示中键入引号字符,仅出于某种原因抓取部分引号。
-
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.
现在你有一个非常长的行...我最终不得不将1个字符串分成大约2000个字符的长行....疯狂的长行是杀死IE和/或打破Java字符串限制。
突出显示LF char并执行Replace All以替换为空字符串
#1
Here's a link which features Crockford's implementation of the quote()
function. Use it to build your own JavaScript converter.
这是一个以Crockford实现quote()函数为特色的链接。用它来构建自己的JavaScript转换器。
Edit: I also slightly modified the function to output an ascii-safe string by default.
编辑:我还稍微修改了函数,默认输出ascii-safe字符串。
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...
Edit2:只是一个建议:将JavaScript保存在外部文件中并在运行时读取而不是硬编码可能会更聪明......
Edit3: And here's a fully-featured solution - just copy to a .html
file and replace the dummy script:
Edit3:这是一个功能齐全的解决方案 - 只需复制到.html文件并替换虚拟脚本:
<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>
#2
You can compress the file using one of the available tools to achieve this effect:
您可以使用其中一个可用工具压缩文件以实现此效果:
YUI压缩机在线
迪安爱德华的包装工
Douglas Crockford的JSMIN
#3
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.
您可以使用jsmin工具将Javascript压缩为单行(希望如此),但它不会转义引号。这可以通过编辑器中的搜索/替换或使用的服务器端脚本语言来完成。
#4
So everything I tried ended up breaking the javascript. I finally got it to work by doing the following:
所以我尝试的一切最终打破了javascript。我终于通过以下方式让它工作:
Using Notepad++:
-
Hit Shift + Tab a bunch of times to unindent every line
点击Shift + Tab多次以取消每一行
-
Do View -> Show End Of Line
查看 - >显示行尾
- Highlight the LF char and do a Replace All to replace with empty string
-
Repeat for the CR char
重复CR char
-
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.
突出显示“(引用字符)并执行全部替换”(转义引号)...只需在“替换”提示中键入引号字符,仅出于某种原因抓取部分引号。
-
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.
现在你有一个非常长的行...我最终不得不将1个字符串分成大约2000个字符的长行....疯狂的长行是杀死IE和/或打破Java字符串限制。
突出显示LF char并执行Replace All以替换为空字符串