0

I have found a LOT of posts that show how to do a search and replace for an exact string between quotes when using a wild card or direct search string. However when using variables in a for loop I am having trouble getting the correct syntax.

I have a number of keys in a json output from a db,

myJson = JSON.stringify(rowsMap);

gives

{
  "myStuff": [
    {
      "abc": "val_de_1",
      "abcxz": "val_xc_1",
      "mnabc": "val_abc_1"
    },
    {
      "abc": "val_de_2",
      "abcxz": "val_xc_2",
      "mnabc": "val_abc_2"
    },
    {
      "abc": "val_de_3",
      "abcxz": "val_xc_3",
      "mnabc": "val_abc_3"
    }
}

Now I would like to loop over the JSON and replace some of the key strings using a 2nd k:v pair

 for (var myVal in myPairs) {
        myJson = myJson.replace(RegExp(myVal, "g"), myPairs[myVal]);
        ....
        }

Now if the values are abc to 777 I get the following,

{
      "myStuff": [
        {
          "777": "val_de_1",
          "777xz": "val_xc_1",
          "mn777": "val_777_1"
        },
        {
          "777": "val_de_2",
          "777xz": "val_xc_2",
          "mn777": "val_777_2"
        },
        {
          "777": "val_de_3",
          "777xz": "val_xc_3",
          "777bc": "val_777_3"
        }
}

but I actually want

{
      "myStuff": [
        {
          "777": "val_de_1",
          "abcxz": "val_xc_1",
          "mnabc": "val_abc_1"
        },
        {
          "777": "val_de_2",
          "abcxz": "val_xc_2",
          "mnabc": "val_abc_2"
        },
        {
          "777": "val_de_3",
          "abcxz": "val_xc_3",
          "mnabc": "val_abc_3"
        }
    }

I have seen the use of brackets braces and variations of quotes before and after a literal string but not a variable.

What is the most efficient method for processing? I have been able to do it in a cluncky manner

for (var myVal in myPairs) {

            myNewVal = "\""+myVal+"\"";
            myNewRep = "\""+myPairs[myVal]+"\"";
            myJson = myJson.replace(RegExp(myNewVal, "g"), myNewRep);
            ....
            }

Note I am looking for a solution where the regexp must look for the exact string between quotes, not include the quotes. The idea is to only do the myNewRep if the string is found, thereby making it faster I assume.

2 Answers 2

4

You can replace the json key string with /[abc]/g

This will replace the exact string.

some matches I tried.

abc - match "abc" - match abcd - Not match

http://www.regexpal.com/ - try this to test.

Sign up to request clarification or add additional context in comments.

Comments

0

You could split the string by quotes

'"abc"'.match(/"(.*?)"/)

1 Comment

this does not work with a variable as per the question. the regexp must look for the exact string between quotes, not include the quotes.

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.