我正在编写一个Java代码,我正在创建一个JavaScript文件。 在脚本文件中,我想写
var alertData = "Following characters are not allowed for value. [ $ & + , / : ; =? @ \" < > # % { } | \\ ~ ^ [ ] ` ]";
当我编译代码并调用方法来创建文件时,
Syntax Error: syntax error: > is not valid.
有人可以帮忙吗?
更多代码
StringBuilder sb = new StringBuilder(100);
sb.append("var validationAlertForValues = \"");
sb.append(("Following characters are not allowed for value. [ $ & + , / : ; =? @ \" < > # % { } | \\ ~ ^ [ ] ` ]"));
sb.append("\";\n var validationAlertForKeys = \"");
sb.append(("Following characters are not allowed for key name. [ $ & + , / : ; =? @ \" # % { } | ~ ^ [ ] ` <space< ] "));
FileWriter fstream = null;
BufferedWriter out = null;
try {
fstream = new FileWriter(filePath);
out = new BufferedWriter(fstream);
out.write(sb.toString());
} catch (Exception e) {
log.error("Exception in making JS", e);
}
答案 0 :(得分:3)
使用此Apache CommonLang StringEscapeUtils。
有些类可以逃避不同类型的输入。这是我发现的一个例子。
import org.apache.commons.lang.StringEscapeUtils;
public class MainClass {
public static void main(String[] args) {
String strHTMLInput = "<P>MyName<P>";
String strEscapeHTML = StringEscapeUtils.escapeHtml(strHTMLInput);
String strUnEscapeHTML = StringEscapeUtils.unescapeHtml(strEscapeHTML);
System.out.println("Escaped HTML >>> " + strEscapeHTML);
System.out.println("UnEscaped HTML >>> " + strUnEscapeHTML);
}
}
答案 1 :(得分:1)
您使用了\"
这是转义符号。尝试使用\\"
。