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 3315452
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:21:07+00:00 2026-05-17T22:21:07+00:00

Possible Duplicate: Regular Expression to escape double quotes inside single quotes I need a

  • 0

Possible Duplicate:
Regular Expression to escape double quotes inside single quotes

I need a regex (no other language!!, best would be perl syntax REGEX or PCRE syntax REGEX) to replace all double quotes " with a \" that are inside a single quoted string. This is an example string (part of a file):

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">';

(Be aware: They dont have to be paired “someValueBetween” so its possible that there are uneven numbers of double quotes in one single quoted string.)

This should be the end result for the last line above:

'<div id=\"buttonbar\" style=\"width:810px; text-align:right\">';

Thanks in advance

***Update:
To make it clear, i want a regular expression only, not a perl programm. The regular expression can be perl regex syntax or PHP PCRE syntax (which is a very close syntax to the perl regex syntax from what i understand). Goal is that you can run the regex in IDES in the search and replace menus that support regex’s (like Eclipse and PhpEd f.e )!!

In other words, i want a regex that i will put in the search IDE field that gives me exactly all unescaped " in the single quoted string as a result. In the replace field of eclipse i can then just put \$1 to escape them.

They should work in Regexbuddy or regex coach please so i can test them.

At least that is the plan 🙂


  • 1 1 Answer
  • 0 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-17T22:21:07+00:00Added an answer on May 17, 2026 at 10:21 pm

    You asked for Perl (or PCRE) and nothing else.

    Ok.

    If you just want to escape unescaped double quotes no matter where you find them, do this:

      s{
          (?<! (?<! \\ ) \\{1} )
          (?<! (?<! \\ ) \\{3} )
          (?<! (?<! \\ ) \\{5} )
          (?<! (?<! \\ ) \\{7} )
          (?= " )
      }{\\}xg;
    

    If you want to escape unescaped double quotes between unescaped single quotes, and you only have one pair of such single quotes, do this:

    1 while s{
    
      (?(DEFINE)
    
        (?<unescaped>
          (?<! (?<! \\ ) \\{1} )
          (?<! (?<! \\ ) \\{3} )
          (?<! (?<! \\ ) \\{5} )
          (?<! (?<! \\ ) \\{7} )
        )
    
        (?<single_quote> (?&unescaped) ' )
        (?<double_quote> (?&unescaped) " )
        (?<unquoted>     [^'] *?          )
    
      )
    
      (?<HEAD>
        (?&single_quote)
        (?&unquoted)
      )
    
      (?<TAIL>
        (?&double_quote)
        (?&unquoted)
        (?&single_quote)
    
      )
    
    }<$+{HEAD}\\$+{TAIL}>xg;
    

    But if you may have multiple sets of paired unescaped single quotes per line, and you only want to escape the unescaped double quotes that fall between those unescaped single quotes, then do this:

    sub escape_quote {
      my $_ = shift;
      s{
          (?<! (?<! \\ ) \\{1} )
          (?<! (?<! \\ ) \\{3} )
          (?<! (?<! \\ ) \\{5} )
          (?<! (?<! \\ ) \\{7} )
          (?= " )
      }{\\}xg;
    
      return $_;
    }
    
    s{
    
      (?(DEFINE)
    
        (?<unescaped>
          (?<! (?<! \\ ) \\{1} )
          (?<! (?<! \\ ) \\{3} )
          (?<! (?<! \\ ) \\{5} )
          (?<! (?<! \\ ) \\{7} )
        )
    
        (?<single_quote> (?&unescaped) ' )
        (?<unquoted>     [^'] *?          )
    
      )
    
      (?<HEAD> (?&single_quote) )
      (?<TARGET> (?&unquoted) )
      (?<TAIL> (?&single_quote) )
    
    }{
                   $+{HEAD}    .
      escape_quote($+{TARGET}) .
                   $+{TAIL}
    
    }xeg;
    

    Note that this all presupposed you have no legitimate paired unescaped double quotes containing unescaped single quotes. Even something like this will throw you off:

    my $cute = q(') . "stuff" . q(');
    

    Probably, though, you want to use a proper parsing module.

    Please pay no attention to all the garish and deceitfully incorrect SO coloring. For some reason, it doesn’t seem to be able to parse Perl as well as perl does. Can’t imagine why. ☺

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

Sidebar

Related Questions

Possible Duplicate: Regular expression to match hostname or IP Address? I need to validate
Possible Duplicate: What is the best regular expression for validating email addresses? I am
Possible Duplicate: Useful Regular Expression Tutorial Hello, I recently started coding in javascript. I
Possible Duplicate: php regular expression help? hi, i want to replace i like apple
Possible Duplicate: Is there a regular expression to detect a valid regular expression? Regular
Possible Duplicate: how can i validate a url in javascript using regular expression I
Possible Duplicate: A comprehensive regex for phone number validation Greetings, I need to implement
Possible Duplicate: .NET - What’s the best way to implement a catch all exceptions
Possible Duplicate: Regular Expressions: Is there an AND operator? I let my users enter
Possible Duplicate: PHP Regular express to remove <h1> tags (and their content) I have

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.