Here you can find the source of escapeJavascript(String str)
Parameter | Description |
---|---|
str | should be encoded |
public static String escapeJavascript(String str)
//package com.java2s; import java.util.HashMap; import java.util.Map.Entry; import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; import javax.script.ScriptException; public class Main { private static final ScriptEngine engine = new ScriptEngineManager().getEngineByName("JavaScript"); private static final HashMap<String, String> jsReplaces = new HashMap<>(); /**//from w w w . j a v a2 s .com * Encoding if need escaping %$&+,/:;=?@<>#% * * @param str should be encoded * * @return encoded Result */ public static String escapeJavascript(String str) { try { String n = engine.eval(String.format("escape(\"%s\")", str.replaceAll("%20", " "))).toString(); for (Entry<String, String> e : jsReplaces.entrySet()) { n = n.replaceAll(e.getKey(), e.getValue()); } return n; } catch (ScriptException ex) { return null; } } }