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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:33:15+00:00 2026-05-27T19:33:15+00:00

I’m trying to come up with a regular expression which will search for a

  • 0

I’m trying to come up with a regular expression which will search for a string and replace with new set of characters.
My string is something like this.

(Product=='Partnership - CT'&&State==CT)

Here I have to search for the CT value for state key and replace with 'CT' (i.e., add single quotation marks). I have used the following Regexp in JavaScript.

var expressionQuery = "(Product=='Partnership - CT'&&State==CT)";
val = 'CT';
expressionQuery.replace(new RegExp(val, "g"),"\'"+val+"\'" );

But this is giving output as (Product=='Partnership - 'CT''&&State=='CT')

Expected output : (Product=='Partnership - CT'&&State=='CT')

That is, I only want to change the instance of CT that appears immediately after ==. Please help me to write the expression to fix this.

  • 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-27T19:33:16+00:00Added an answer on May 27, 2026 at 7:33 pm

    UPDATE: You don’t have to hardcode the “CT” in your regular expression. If you know that the “State” parameter will always be a two-letter code you can add quotation marks to any state with something like the following:

    expressionQuery.replace(/State==([A-Z]{2})/g,"State=='$1'");
    

    This says to find a the substring “State==” followed by any two letters, and then in the replace expression where it has “$1” that means insert whatever matched the part of the regex in parentheses. Note that because it specifically looks for a letter after “==” it won’t add extra quotation marks if they were already present.

    And my original answer, which replaces only the specified state (whether hardcoded or otherwise indicated by a variable):

    You could go for non-capturing matches on the “==” and so forth, but I think it would be easier to just include “==” in both the search and replace text:

    var expressionQuery = "(Product=='Partnership - CT'&&State==CT)",
        val = "CT";
    
    expressionQuery.replace(new RegExp("==" + val, "g"),"=='"+val+"'" );
    

    Or you may even want to include “State” in the search, in case you later have other parameters that might match, like “SomeOtherParam==CT”:

    expressionQuery.replace(new RegExp("State==" + val, "g"),"State=='"+val+"'" );
    

    Note that you don’t need to escape single quotation marks if they are in a string quoted with doubles. I.e., "'" does the same thing as "\'".

    (Note also that your original code was invalid because you forgot to put any quotation marks around the string in your declaration of expressionQuery.)

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words

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.