<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
	<channel>
		<title><![CDATA[JavaRanch: Latest posts for the topic "Problem escaping single quotes using regex with replaceAll()"]]></title>
		<link>http://www.coderanch.com/forums/t/1/Java-General/escaping-single-quotes-using-regex</link>
		<description><![CDATA[Latest messages posted in the topic "Problem escaping single quotes using regex with replaceAll()"]]></description>
		<generator>JForum - http://www.jforum.net</generator>
			<item>
				<title>Problem escaping single quotes using regex with replaceAll()</title>
				<description><![CDATA[Hi All,<br />    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.<br />  <br /> Rob<br /> [ May 29, 2007: Message edited by: Bear Bibeault ]<br /> ]]></description>
				<guid isPermaLink="true">http://www.coderanch.com/forums/posts/preList/382780/1671976</guid>
				<link>http://www.coderanch.com/forums/posts/preList/382780/1671976</link>
				<pubDate><![CDATA[Tue, May 29 2007 12:34:00 MDT]]></pubDate>
				<author><![CDATA[Rob Hunter]]></author>
			</item>
			<item>
				<title>Problem escaping single quotes using regex with replaceAll()</title>
				<description><![CDATA[There is no builtin means to convert a string to its JavaScript-escaped equivalent.<br />  <br /> What I've done is to create a <a href="http://www.javaranch.com" class="faq" title="A Friendly Place for Java Greenhorns" target="_new">Java</a> class that can be used from anywhere that escapes all quotes in a String making it suitable for use as a JavaScript string literal.<br />  <br /> 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.<br />  <br /> I generally perfer the latter since the decision to escape or not is usually a display decision that's best handled in the JSP.]]></description>
				<guid isPermaLink="true">http://www.coderanch.com/forums/posts/preList/382780/1671977</guid>
				<link>http://www.coderanch.com/forums/posts/preList/382780/1671977</link>
				<pubDate><![CDATA[Tue, May 29 2007 12:45:00 MDT]]></pubDate>
				<author><![CDATA[Bear Bibeault]]></author>
			</item>
			<item>
				<title>Problem escaping single quotes using regex with replaceAll()</title>
				<description><![CDATA[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.<br />  <br /> Rob]]></description>
				<guid isPermaLink="true">http://www.coderanch.com/forums/posts/preList/382780/1671978</guid>
				<link>http://www.coderanch.com/forums/posts/preList/382780/1671978</link>
				<pubDate><![CDATA[Tue, May 29 2007 12:54:00 MDT]]></pubDate>
				<author><![CDATA[Rob Hunter]]></author>
			</item>
			<item>
				<title>Problem escaping single quotes using regex with replaceAll()</title>
				<description><![CDATA[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.<br />  <br /> Rob]]></description>
				<guid isPermaLink="true">http://www.coderanch.com/forums/posts/preList/382780/1671979</guid>
				<link>http://www.coderanch.com/forums/posts/preList/382780/1671979</link>
				<pubDate><![CDATA[Tue, May 29 2007 12:59:00 MDT]]></pubDate>
				<author><![CDATA[Rob Hunter]]></author>
			</item>
			<item>
				<title>Problem escaping single quotes using regex with replaceAll()</title>
				<description><![CDATA[It sounds like you are having trouble performing the substitution using a regex? Correct? <br />  <br /> Code?]]></description>
				<guid isPermaLink="true">http://www.coderanch.com/forums/posts/preList/382780/1671980</guid>
				<link>http://www.coderanch.com/forums/posts/preList/382780/1671980</link>
				<pubDate><![CDATA[Tue, May 29 2007 13:01:00 MDT]]></pubDate>
				<author><![CDATA[Bear Bibeault]]></author>
			</item>
			<item>
				<title>Problem escaping single quotes using regex with replaceAll()</title>
				<description><![CDATA[<pre>
City_Name = City_Name.replaceAll(&quot;'&quot;, &quot;\\\'&quot;);  //JSP Code to put the string in the javascript
 
onklick=&quot;callFunct('&lt;%=City_Name');&quot;
 </pre><br />  <br /> This is essentially what I'm trying to do. But the quotes are fooling it up.<br />  <br /> Rob]]></description>
				<guid isPermaLink="true">http://www.coderanch.com/forums/posts/preList/382780/1671981</guid>
				<link>http://www.coderanch.com/forums/posts/preList/382780/1671981</link>
				<pubDate><![CDATA[Tue, May 29 2007 13:09:00 MDT]]></pubDate>
				<author><![CDATA[Rob Hunter]]></author>
			</item>
			<item>
				<title>Problem escaping single quotes using regex with replaceAll()</title>
				<description><![CDATA[Add the %&gt; after City_Name for the scriptlet part as well (just jotted it down quick, never copied it).]]></description>
				<guid isPermaLink="true">http://www.coderanch.com/forums/posts/preList/382780/1671982</guid>
				<link>http://www.coderanch.com/forums/posts/preList/382780/1671982</link>
				<pubDate><![CDATA[Tue, May 29 2007 13:11:00 MDT]]></pubDate>
				<author><![CDATA[Rob Hunter]]></author>
			</item>
			<item>
				<title>Problem escaping single quotes using regex with replaceAll()</title>
				<description><![CDATA[I've updated the title of this topic and moved it to a general <a href="http://www.javaranch.com" class="faq" title="A Friendly Place for Java Greenhorns" target="_new">Java</a> forum which is a better place to discuss use of replaceAll() and regular expressions.]]></description>
				<guid isPermaLink="true">http://www.coderanch.com/forums/posts/preList/382780/1671983</guid>
				<link>http://www.coderanch.com/forums/posts/preList/382780/1671983</link>
				<pubDate><![CDATA[Tue, May 29 2007 13:15:00 MDT]]></pubDate>
				<author><![CDATA[Bear Bibeault]]></author>
			</item>
			<item>
				<title>Problem escaping single quotes using regex with replaceAll()</title>
				<description><![CDATA[When you specify the replacement string, it's going to be parsed first by the <a href="http://www.javaranch.com" class="faq" title="A Friendly Place for Java Greenhorns" target="_new">Java</a> compiler, and then by the regular expression interpreter.  Now let's work backwards.  What you want the replacement to be is <b>\'</b>.  Now the <b>'</b> 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 <b>'</b> can be represented as is, no problem.  But the <b>\</b> is a problem.  It's a special character to a regex parser, so if you want a 'real' <b>\</b> you need to escape it with another <b>\</b>.  So the regex parser needs to see <b>\\</b>.  But before that, the javac compiler also treats <b>\</b> as a special character, so each <b>\</b> needs to be doubled again for the compiler.  Thus <b>\\</b> needs to become <b>\\\\</b> when it's inside a string literal.<br />  <br /> The end result, then, is that the code you want is<br /> <pre>City_Name = City_Name.replaceAll("'", "\\\\'");</pre>]]></description>
				<guid isPermaLink="true">http://www.coderanch.com/forums/posts/preList/382780/1671984</guid>
				<link>http://www.coderanch.com/forums/posts/preList/382780/1671984</link>
				<pubDate><![CDATA[Tue, May 29 2007 15:28:00 MDT]]></pubDate>
				<author><![CDATA[Jim Yingst]]></author>
			</item>
			<item>
				<title>Problem escaping single quotes using regex with replaceAll()</title>
				<description><![CDATA[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.]]></description>
				<guid isPermaLink="true">http://www.coderanch.com/forums/posts/preList/382780/1671985</guid>
				<link>http://www.coderanch.com/forums/posts/preList/382780/1671985</link>
				<pubDate><![CDATA[Wed, May 30 2007 11:51:00 MDT]]></pubDate>
				<author><![CDATA[Rob Hunter]]></author>
			</item>
			<item>
				<title>Problem escaping single quotes using regex with replaceAll()</title>
				<description><![CDATA[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?<br /> <pre>    public static void main(String[] args) {
        String input = &quot;It's \&quot;its\&quot; not \&quot;it's\&quot;.&quot;;
        String output = input.replaceAll(&quot;'&quot;, &quot;\\\\'&quot;);
        System.out.println(output);
    }</pre>]]></description>
				<guid isPermaLink="true">http://www.coderanch.com/forums/posts/preList/382780/1671986</guid>
				<link>http://www.coderanch.com/forums/posts/preList/382780/1671986</link>
				<pubDate><![CDATA[Wed, May 30 2007 14:13:00 MDT]]></pubDate>
				<author><![CDATA[Jim Yingst]]></author>
			</item>
			<item>
				<title>Problem escaping single quotes using regex with replaceAll()</title>
				<description><![CDATA[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.<br />  <br /> Rob]]></description>
				<guid isPermaLink="true">http://www.coderanch.com/forums/posts/preList/382780/1671987</guid>
				<link>http://www.coderanch.com/forums/posts/preList/382780/1671987</link>
				<pubDate><![CDATA[Thu, May 31 2007 09:54:00 MDT]]></pubDate>
				<author><![CDATA[Rob Hunter]]></author>
			</item>
	</channel>
</rss>
