Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3287034
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:26:52+00:00 2026-05-17T20:26:52+00:00

I would need a regular expression that escapes or captures (if not already escaped)

  • 0

I would need a regular expression that escapes or captures (if not already escaped) ALL the double quote characters INSIDE a single quoted string and then convert the opening single quotes to double quotes!

We are refactoring files that have a lot (and i mean a lot!) of single quoted strings in either PHP and also JS files. The only thing they have in common is that the strings are at least in one line and are concated with = in both languages.

I give an example (the example is ugly legacy code so dont judge it please, i already did this 🙂 )
We have a file that starts like this:

var baseUrl = $("#baseurl").html();
var head = '<div id="finishingDiv" style="background-image:url({baseUrl}css/userAd/images/out_main.jpg); background-repeat: repeat-y; ">'+
'<div id="buttonbar" style="width:810px; text-align:right">';

and i want it to look like this:

var baseUrl = $("#baseurl").html();
var head = "<div id=\"finishingDiv\" style=\"background-image:url({baseUrl}css/userAd/images/out_main.jpg); background-repeat: repeat-y; \">" +
"<div id=\"buttonbar\" style=\"width:810px; text-align:right\">";

As you see the correct double quote strings are not touched.

So my basic question: How do i capture all characters of one kind (in my case the character ” ) between a certain start and end character (in my case the character ‘ ).

This regex '.*(").*' or '[^']*(")[^']*' just captures always one ” for me per match. If if needs more than one step its also ok, it should just work.
I would be happy of any solution, IDE specific, language specific or shell specific, that acutally works.

Please help, im desperate, thanks a lot

  • 1 1 Answer
  • 3 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T20:26:52+00:00Added an answer on May 17, 2026 at 8:26 pm

    The biggest problem is going to be figuring out where all the strings are, since you can’t parse all of JS or PHP with a regex. However, if I assume that you don’t care about comments, this Ruby code will catch most cases (but you should review its output):

    #!/usr/bin/ruby -p
    
    gsub!(/'((?:[^\\']|\\[\\'])+)'/) do |m|
      %Q{"#{$1.gsub("\\'","'").gsub(/\\[^\\]/) { "\\#{$0}" }.gsub('"','\\"')}"}
    end
    

    This code takes whatever’s presented on stdin / the contents of the file arguments, finds a single-quoted string (taking into account the possible presence of \\ and \'), and then, for its replacement, runs a series of substitutions within the matched string (sanitizing backslashes, etc.). The result is printed to stdout. If you want a more automated approach, replace the first line with #!/usr/bin/ruby -pi.bak; then, whatever file arguments are presented have the substitution run on them destructively in-place. The old files are kept with an additional .bak extension.

    To run this code, if you haven’t used Ruby before: save it as anything, such as fix-sq.rb; run chmod +x fix-sq.rb; and then run ./fix-sq.rb file1 file2 file3.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would need to get a Regular Expression, which matches all Unicode control characters
I need to use a regular expression that contains all the \b characters except
I need a regular expression that would let me validate Url. I found this
I need to find a regular expression that would be able to work around
Possible Duplicate: Regular Expression to escape double quotes inside single quotes I need a
I need a regular expression that would split strings like itemXY where X and
I'm trying to work out what regular expression I would need to change this
I need a regular expression that detects timer strings that could be less than
I need a regular expression in MySQL that matches: a sentence that has a
I need a regular expression that will match this pattern (case doesn't matter): 066B-E77B-CE41-4279

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.