1

I have a ruby string which includes many escaped double quotes \" that I want to replace with an escaped single quote \' but I can't work out how to do it.

I've been trying to use gsub, but something like mystring.gsub('\\"', '\\'') doesn't work.

Sample of (part of) the string below - my problem is that I don't want to replace all the double quotes, just the escaped ones...

FYI, the injected script works fine in included as src, but I'm pulling the content dynamically and injecting into the script in the view:

instead of

<script src='...'></script> 

Im doing

<script><%=raw @mystring%></script>

....

document.write("<div class=\"fsBody fsEmbed\">"+"\n");
document.write("<link rel=\"stylesheet\" type=\"text/css\" href=\"https://www.formstack.com/forms/css/3/reset.css?20140508\" />"+"\n");
document.write("    <link rel=\"stylesheet\" type=\"text/css\" href=\"https://www.formstack.com/forms/css/3/default.css?20140519\" />"+"\n");
document.write("    "+"\n");
document.write("<!--[if IE]>"+"\n");
document.write("    <link rel=\"stylesheet\" type=\"text/css\" media=\"all\" href=\"https://www.formstack.com/forms/css/3/ie.css?20140508\" />"+"\n");
document.write("<![endif]-->"+"\n");
document.write("<!--[if IE 7]><link rel=\"stylesheet\" type=\"text/css\" media=\"all\" href=\"https://www.formstack.com/forms/css/3/ie7.css\" /><![endif]-->"+"\n");
document.write("<!--[if IE 6]><link rel=\"stylesheet\" type=\"text/css\" media=\"all\" href=\"https://www.formstack.com/forms/css/3/ie6fixes.css\" /><![endif]-->"+"\n");
document.write("    <style type=\"text/css\">"+"\n");
document.write("        "+"\n");
document.write("    .fsBody .fsForm, .fsForm .fsSpacerRow .fsRowBody {"+"\n");

Can someone help?

4
  • Are you sure they're escaped? Rails will render them with leading "\" in certain circumstances, like when you inspect a variable or output it in irb. Commented Aug 7, 2014 at 22:13
  • If they are escaped, try this: mystring.gsub('\\"', '\\\''). You first have to escape the slash, then escape the quote. Commented Aug 7, 2014 at 22:15
  • @Adam - You don't need to escape ` or "` when using single quotes. You only need to escape ' or \` if proceeding '`. Commented Aug 7, 2014 at 22:17
  • definitely escaped - here is a sample of the string output using <%=raw %> (will insert into the original question) Commented Aug 7, 2014 at 22:18

2 Answers 2

1

Most likely they are not escaped. This is just the way ruby displays strings with quotes when you execute inspect on them (console execute inspect by default to display results of commands). Try:

mystring.gsub('"', "'")
Sign up to request clarification or add additional context in comments.

Comments

0

In the end, I wound up using a prerender server to prerender the page and include the contents of the script.

I got to the point where if the script was included from src url it worked, but if I tried to pull in the source and including it in the page between tags it wouldn't work.

Using prerender allowed me to run the script on the server side and generate the page before returning completed partial to the browser.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.