I have to call some Javascript functions from my Java code and some strings as parameter. The string may contain new line and double/single quote. So I want to escape the new line and double quote using java so that it can be passed smoothly to JS method? How to do the same?
Asked
Active
Viewed 964 times
-1
-
http://stackoverflow.com/questions/1265282/recommended-method-for-escaping-html-in-java – brso05 May 05 '15 at 13:39
-
Would you mind sharing what you have tried so far? – Anthony Forloney May 05 '15 at 13:39
-
We could really use some examples here. – Alan Moore May 06 '15 at 01:29
2 Answers
0
Use this: st.replaceAll("\\s+","")
- removes all whitespaces and non visible characters such as tab, \n
and for removing single quotes use str.replace("'", "")
and so on for similar things.

Brane
- 3,257
- 2
- 42
- 53
-
I do want to replace the those characters, I need them in javascript, so I want to just escape them. – dev_android May 06 '15 at 05:35
0
I tried Apache common lib. StringEscapeUtils.escapeJavaScript(String)
using org.apache.commons.lang.StringEscapeUtils

dev_android
- 8,698
- 22
- 91
- 148