0

In my code I read a variable from JSON file. The Variable is a $ sign.

It looks like:

  "jsonTest":{
    "doJson":"true",
    "steps":[
      {
        "path": "$",
        "compare": "test"
      }
    ]
  }

No I read this variable with JSON Path and do an console.log

console.log(entry.path);

Result is the Expected $

Now I added this to a string:

addJsonTests += '   .expect(await getJSONInfo(\''+ entry.path +'\')).eql(\''+ entry.compare +'\',\'JSON Wertvergleich fehlgeschlagen\', {timeout: 40000}) \n';

Text I got back in console.log is:

.expect(await getJSONInfo('$')).eql('test','JSON Wertvergleich fehlgeschlagen', {timeout: 40000})

So here the $ is still existing. Now I replace this text with a placeholder and write it back to a physical file. Now the physical file looks like:

        await t 
   .expect(await getJSONInfo('

    });)).eql('test','JSON Wertvergleich fehlgeschlagen', {timeout: 40000}) 

The $ sign is gone. I think it will be lost in replace function. This looks like:

tempData = tempData.replace('%%JSONTest%%',addJsonTests);

After this line the string is damaged. Any idea why this happen with $ sign?

ingo
  • 776
  • 1
  • 10
  • 25

1 Answers1

1

Ok, it is the special pattern issue. To get a $ in the replace method it need to get a $$ in the input string.

Seen on: JavaScript replace() method dollar signs

ingo
  • 776
  • 1
  • 10
  • 25