The moose likes Java in General and the fly likes Problem escaping single quotes using regex with replaceAll() Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Problem escaping single quotes using regex with replaceAll()" Watch "Problem escaping single quotes using regex with replaceAll()" New topic
Author

Problem escaping single quotes using regex with replaceAll()

Rob Hunter
Ranch Hand

Joined: Apr 09, 2002
Posts: 764
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 opinionated walrus
Marshal

Joined: Jan 10, 2002
Posts: 50691

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.


[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
Rob Hunter
Ranch Hand

Joined: Apr 09, 2002
Posts: 764
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

Joined: Apr 09, 2002
Posts: 764
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 opinionated walrus
Marshal

Joined: Jan 10, 2002
Posts: 50691

It sounds like you are having trouble performing the substitution using a regex? Correct?

Code?
Rob Hunter
Ranch Hand

Joined: Apr 09, 2002
Posts: 764


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

Rob
Rob Hunter
Ranch Hand

Joined: Apr 09, 2002
Posts: 764
Add the %> after City_Name for the scriptlet part as well (just jotted it down quick, never copied it).
Bear Bibeault
Author and opinionated walrus
Marshal

Joined: Jan 10, 2002
Posts: 50691

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

Joined: Jan 30, 2000
Posts: 18652
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


"I'm not back." - Bill Harding, Twister
Rob Hunter
Ranch Hand

Joined: Apr 09, 2002
Posts: 764
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

Joined: Jan 30, 2000
Posts: 18652
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

Joined: Apr 09, 2002
Posts: 764
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
 
 
subject: Problem escaping single quotes using regex with replaceAll()
 
Threads others viewed
String replace function with single quote
mysql select exception
javascript syntax error
Why does this throw an exception?
Escape characters in JDBC (single quote/apostrophe)
WebSphere development made easy
without the weight of IBM tools
http://www.myeclipseide.com

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture