[英]Android escape JavaScript String
I'm appending a JavaScript to an HTML file using android. 我正在使用android将JavaScript添加到HTML文件中。 I have this part: 我有这个部分:
String JavaScriptStr = "this.value = this.value.replace(/[^0-9\.]/g,'');";
Eclipse gives me an error saying: Eclipse给我一个错误说:
Invalid escape sequence (valid ones are \b \t \n, etc...)
How do I escape that JS string? 如何逃避JS字符串? Thanks for any help! 谢谢你的帮助!
你必须逃避\\
,试试
String JavaScriptStr = "this.value = this.value.replace(/[^0-9\\.]/g,'');";
There is a class called StringEscapeUtils in common langs api, which will provide you several methods like escapeHTML and escapeJavascript to escape html and javascript respectively. 在常见的langs api中有一个名为StringEscapeUtils的类,它将为您提供几个方法,如escapeHTML和escapeJavascript,分别用于转义html和javascript。 Otherswise you can also use solution suggested by xdazz. 另外,您也可以使用xdazz建议的解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.