• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem escaping single quotes using regex with replaceAll()  RSS feed

 
Rob Hunter
Ranch Hand
Posts: 832
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
Here's the issue. I have strings coming in that may have single quotes in them. I need to print these strings out onto a page as parameters to particular javascript functions. If I have onclick with the function call wrapped in double quotes and the parameter wrapped in single quotes I need to have this string escaped (i.e. the single or double quotes). How can I do this? I've tried a few things but none seem to work. JDK 1.5 is what I'm using. Thanks.

Rob
[ May 29, 2007: Message edited by: Bear Bibeault ]
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66305
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no builtin means to convert a string to its JavaScript-escaped equivalent.

What I've done is to create a Java class that can be used from anywhere that escapes all quotes in a String making it suitable for use as a JavaScript string literal.

This class' method can be used in a page controller in order to modify the string prior to sending it off to the JSP. But I also used it to define an EL function that can be used directly on the page.

I generally perfer the latter since the decision to escape or not is usually a display decision that's best handled in the JSP.
 
Rob Hunter
Ranch Hand
Posts: 832
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't just sent a backslash escaped followed by a single quote to the page? If I were to escape a single quote in the function call it should work shouldn't it? I though by putting 2 backslashes and the a single quote it would take. Then I read a little about the Matcher class and (I guess) the double-parse so I also tried 4 backslashes, and then 5, followed by a single quote and still nada. How come doing it this way doesn't work? Thanks again.

Rob
 
Rob Hunter
Ranch Hand
Posts: 832
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't just sent a backslash escaped followed by a single quote to the page? If I were to escape a single quote in the function call it should work shouldn't it? I though by putting 2 backslashes and the a single quote it would take. Then I read a little about the Matcher class and (I guess) the double-parse so I also tried 4 backslashes, and then 5, followed by a single quote and still nada. How come doing it this way doesn't work? Thanks again.

Rob
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66305
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It sounds like you are having trouble performing the substitution using a regex? Correct?

Code?
 
Rob Hunter
Ranch Hand
Posts: 832
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This is essentially what I'm trying to do. But the quotes are fooling it up.

Rob
 
Rob Hunter
Ranch Hand
Posts: 832
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Add the %> after City_Name for the scriptlet part as well (just jotted it down quick, never copied it).
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66305
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've updated the title of this topic and moved it to a general Java forum which is a better place to discuss use of replaceAll() and regular expressions.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you specify the replacement string, it's going to be parsed first by the Java compiler, and then by the regular expression interpreter. Now let's work backwards. What you want the replacement to be is \'. Now the ' is not a problem, because that character has no special significance inside regular expressions, and also doesn't need to be escaped inside a string literal (for the compiler). So the ' can be represented as is, no problem. But the \ is a problem. It's a special character to a regex parser, so if you want a 'real' \ you need to escape it with another \. So the regex parser needs to see \\. But before that, the javac compiler also treats \ as a special character, so each \ needs to be doubled again for the compiler. Thus \\ needs to become \\\\ when it's inside a string literal.

The end result, then, is that the code you want is
 
Rob Hunter
Ranch Hand
Posts: 832
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tried that before posting and it doesn't work. I tried double backslash before learning about the extra parsing. I then tried 4 backslashes as you have mentioned and it didn't work. Then I tried the 4 backslashes + 1 to escape the single quote if need be and it didn't work either.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, in what way does the 4-backslash version not work? You can run this simple test to see if it is performing the desired substitution or not. It looks to me like each ' is replaced with \'. Is that not right? Do you need some other replacements as well?
 
Rob Hunter
Ranch Hand
Posts: 832
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a string in JSP such as St. John's, for instance. I need to put this string in the generated HTML page inside of a javascript call. Since the onclick's action is wrapped in double quotes and the parameter in the javascript is wrapped in single quotes I need to escape that string. I've used the 4 backslashes and was "expecting" to see a \' when viewing the page's source but, unforunately, I see just the '. I've tried the replace on the single quote with other strings (i.e. rrrr or even \\\\'eeeee) and the substitution does take place just no preservation of the backslashes. Can anyone suggest another way to substitute or maybe even another approach to getting the string acceptable in the javascript call? Thanks again.

Rob
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!