Skip to content Skip to sidebar Skip to footer

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 sa

Solution 1:

You have to escape \, Try

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

Solution 2:

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.

Post a Comment for "Android Escape Javascript String"