• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
david arnold
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64994
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As the backslash is the escape character, it itself must be escaped in order to be used.
 
david arnold
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64994
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64994
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It has been sovled, thanks!
 
david arnold
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you Bear Bibeault.

I solved the backslash thing by adding double bachslash in stead of using one single backslash.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic