0

I am searching on a list by value on jquery and did this to escape spaces and find an input based on its values:

value.replace(/([^A-Z0-9])/g, '\\$1'))

but, after the page is rendered by the server, the result script end up as

value.replace(/([^A-Z0-9])/g, '\$1'))

notice the replace value has now only one \, and it doesn't work as I wanted.

Why the page rendering does that? If I move the JavaScript out of the jsp page, will it still do that once imported into the page?

Adriano
  • 389
  • 3
  • 11

2 Answers2

0

Jsp's run on the server side, so if you move the js into a separate file, then you're fine, because the browser will be loading the js from the client side.

To answer the other part of your question about jsp rendering, is the above code inside a Java string? Java also uses the \ backslash to escape characters in strings.

0

This previous answer might help you with properly escaping slashes and other special characters from your JS strings in your JSP scripts.

You can either use the functions provided in Apache StringEscapeUtils.

Or you can just replace all double slashes with quadruple slashes in your js code inside your jsp script.

Like so for example:

var jsVariable = "${fn:replace(javaVariable, '\\', '\\\\')}";

Community
  • 1
  • 1
Hassaan
  • 932
  • 8
  • 16