简体   繁体   中英

Android escape JavaScript String

I'm appending a JavaScript to an HTML file using android. I have this part:

String JavaScriptStr = "this.value = this.value.replace(/[^0-9\.]/g,'');";

Eclipse gives me an error saying:

Invalid escape sequence (valid ones are \b \t \n, etc...)

How do I escape that JS string? 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. Otherswise you can also use solution suggested by xdazz.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM