This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Struts and the fly likes backslash is removed from the parameter when javascript function get the parameter from java call Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Frameworks » Struts
Bookmark "backslash is removed from the parameter when javascript function get the parameter from java call" Watch "backslash is removed from the parameter when javascript function get the parameter from java call" New topic
Author

backslash is removed from the parameter when javascript function get the parameter from java call

david arnold
Ranch Hand

Joined: Apr 10, 2009
Posts: 133
Hi there,

I want to use javascript .test function to validate the input data using regex pattern, but when I transfer the pattern in jsp file to javascipt function, the backslash disappeared. For example, in code



The test function does not work, and i found that the pattern is [\p{Alpha}\._ -]{0,31} in jsp file, but when javascript function doValidation got it, the pattern changed to [p{Alpha}._ -]{0,31}, the two backslash was removed.

I do not know how to sovle this problem, please help if anyone know it! Thanks for the help!

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61309
    
  66

As the backslash is the escape character, it itself must be escaped in order to be used.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
david arnold
Ranch Hand

Joined: Apr 10, 2009
Posts: 133
Thanks for the reply, Bear Bibeault.

I just got to know regex, from my knowledge, the backslash is part of some regex pattern, if it is removed, then the function related will not work. Since there will be different kinds pattern when the page load, I could not manually add the backslash to each pattern.

So any solution to my problem?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61309
    
  66

I did not say to remove it. I said to escape it.

So, how does one escape special characters in Java and JavaScript strings?
david arnold
Ranch Hand

Joined: Apr 10, 2009
Posts: 133
Thanks. Now I have change all regex pattern that contains "\" to "\\" in java class (eg. [\\p{Alpha}\\._ -]{0,31} ) , but still, when I call javascript and transfer the pattern to it ,



The alert message still shows that patten is [p{Alpha}._ -]{0,31} , I did not know if i explain it clearly, if the pattern can not have that "\" in it as originally [\p{Alpha}\._ -]{0,31}, then I can not use .test function to do validation. Got stuck in it.....
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61309
    
  66

As this is not a JavaScript issue, but is likely an issue with the write tag, moving to the Struts forum.
david arnold
Ranch Hand

Joined: Apr 10, 2009
Posts: 133
It has been sovled, thanks!
david arnold
Ranch Hand

Joined: Apr 10, 2009
Posts: 133
thank you Bear Bibeault.

I solved the backslash thing by adding double bachslash in stead of using one single backslash.
 
GeeCON Prague 2014
 
subject: backslash is removed from the parameter when javascript function get the parameter from java call