Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative to using StringEscapeUtils.escapeJavaScript() in commons lang3

Tags:

I've been tasked with updating our code from using org.apache.commons.lang to org.apache.commons.lang3 and I've found that the newer version of StringEscapeUtils no longer has the method escapeJavaScript() however we were using this in quite a few places throughout our code.

I've been reading through the documentation and it seems that the whole of StringEscapeUtils was rewritten for lang3 (see release notes lang 3.3.2) and with this rewrite they removed escapeJavaScript() however they haven't said what to use as an alternative in any of their documentation (Not that I can see anyway). Here's the what's new documentation.

So my question is I can't be the only one to have noticed this and experienced this issue so what is the alternative to using StringEscapeUtils.escapeJavaScript()?

like image 260
Popeye Avatar asked Mar 19 '15 13:03

Popeye


People also ask

Is Org Apache Commons Lang StringEscapeUtils deprecated?

@user3871754 No, it is deprecated in the org. apache.

What is StringEscapeUtils escapeJava?

escapeJava. public static String escapeJava(String str) Escapes the characters in a String using Java String rules. Deals correctly with quotes and control-chars (tab, backslash, cr, ff, etc.) So a tab becomes the characters '\\' and 't' .

What is StringEscapeUtils escapeHtml?

Use: StringEscapeUtils.escapeHtml("I'm coder") Escapes the characters in a String using HTML entities. For example: "bread" & "butter"


2 Answers

Either of escapeEcmaScript or escapeJson would be a suitable replacement.

like image 76
Ian Roberts Avatar answered Sep 29 '22 08:09

Ian Roberts


According to the Apache Commons deprecated page, we should be using:

  • Apache Commons Text
    • StringEscapeUtils
    • for Maven/Gradle: 'org.apache.commons:commons-text:'
like image 25
will Avatar answered Sep 29 '22 09:09

will