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

  • Home
  • SEARCH
  • 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 3635630
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T00:55:42+00:00 2026-05-19T00:55:42+00:00

I need to match and replace some comments. for example: $test = the url

  • 0

I need to match and replace some comments.
for example:

$test = "the url is http://www.google.com";// comment "<-- that quote needs to be matched

I want to match the comments outside of the quotes, and replace any "‘s in the comments with &quot;‘s.

I have tried a number of patterns and different ways of running them but with no luck.

The regex will be run with javascript to match php “//” comments

UPDATE:
I took the regex from borkweb below and modified it. used a function from http://ejohn.org/blog/search-and-dont-replace/ and came up with this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <script type="text/javascript">
            function t_replace(data){
               var q = {}, ret = "";
                data.replace(/(?:((["'\/]*(("[^"]*")|('[^']*'))?[\s]*)?[\/\/|#][^"|^']*))/g, function(value){
                    q[key] = value;
                });
                for ( var key in q ){
                    ret =  q[key];
                }
                var text = data.split(ret);
                var out = ret + text[1];
                out = out.replace(/"/g,"&quot;");
                out = out.replace(/'/g,"&apos;");
                return text[0] + out;
            }
        </script>
    </head>
    <body>
        <script type="text/javascript">
            document.write(t_replace("$test = \"the url is http://www.google.com\";// c'o\"mment \"\"\"<-- that quote needs to be matched")+"<br>");
            document.write(t_replace("$test = 'the url is http://www.google.com';# c'o\"mment \"\"\"<-- that quote needs to be matched"));
        </script>
    </body>
</html>

it handles all the line comments outside of single or double quotes. Is there anyway I could optimize this function?

UPDATE 2:
it does not handle this string

document.write(t_replace("$test //= \"the url is http://www.google.com\"; //c'o\"mment \"\"\"<-- that quote needs to be matched")+"<br>");
  • 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-19T00:55:42+00:00Added an answer on May 19, 2026 at 12:55 am

    You can have a regexp to match all strings and comments at the same time. If it’s a string, you can replace it with itself, unchanged, and then handle a special case for comments.

    I came up with this regex:

    "(\\[\s\S]|[^"])*"|'(\\[\s\S]|[^'])*'|(\/\/.*|\/\*[\s\S]*?\*\/)
    

    There are 3 parts:

    • "(\\[\s\S]|[^"])*" for matching double quoted strings.
    • '(\\[\s\S]|[^'])*' for matching single quoted strings.
    • (\/\/.*|\/\*[\s\S]*?\*\/) for matching both single line and multiline comments.

    The replace function check if the matched string is a comment. If it’s not, don’t replace. If it is, replace " and '.

    function t_replace(data){
        var re = /"(\\[\s\S]|[^"])*"|'(\\[\s\S]|[^'])*'|(\/\/.*|\/\*[\s\S]*?\*\/)/g;
        return data.replace(re, function(all, strDouble, strSingle, comment) {
            if (comment) {
                return all.replace(/"/g, '&quot;').replace(/'/g, '&apos;');
            }
            return all;
        });
    }
    

    Test run:

    Input: $test = "the url is http://www.google.com";// c'o"mment """<-- that quote needs to be matched
    Output: $test = "the url is http://www.google.com";// c&apos;o&quot;mment &quot;&quot;&quot;<-- that quote needs to be matched
    
    Input: $test = 'the url is http://www.google.com';# c'o"mment """<-- that quote needs to be matched
    Output: $test = 'the url is http://www.google.com';# c'o"mment """<-- that quote needs to be matched
    
    Input: $test //= "the url is http://www.google.com"; //c'o"mment """<-- that quote needs to be matched
    Output: $test //= &quot;the url is http://www.google.com&quot;; //c&apos;o&quot;mment &quot;&quot;&quot;<-- that quote needs to be matched
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a Regex that will match a java method declaration. I have come
I need to write a regular expression that finds javascript files that match <anypath><slash>js<slash><anything>.js
I need to replace several URLs in a text file with some content dependent
I need to handle HTTP URLs that begin with a certain domain with my
I need to match and remove all tags using a regular expression in Perl.
I need to match something in the form <a href=pic/5 id=piclink><img src=thumb/5 /></a> to
I need to match (case insensitive) abcd and an optional trademark symbol Regex: /abcd(™)?/gi
I need to match a string holiding html using a regex to pull out
I need to match up two almost-the-same long freetext strings; i.e., to find index-to-index
I need a Perl regular expression to match a string. I'm assuming only double-quoted

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.