0

Trying to replace a string that contains special characters. The purpose of this is to convert the query string into an understandable format for end users.

full string is:

var str = 'active=true^opened_by=6816f79cc0a8016401c5a33be04be441^ORassigned_to!=6816f79cc0a8016401c5a33be04be441^short_descriptionISNOTEMPTY^NQopened_atONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()^EQ';

Specifically the portion after ^NQ, in this example: opened_atONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday(). I have split the original string with indexOf(^NQ) and passing the resulting sub-strings to a function. I'm then trying a .replace() as below:

var today = replacementString.replace(/(ONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday())/g, ' is today ');
replacementString = today;

I have tried with various combinations of the above line, but have not returned what I am hoping for.

I've had no issues replacing special characters, or strings without special characters, but the combination of the 2 is confusing/frustrating me.

Any suggestions or guidance would be appreciated

1 Answers1

1

You should escape the () to \(\) to match it literally or else it would mean a capturing group. For the match you could also omit the outer parenthesis and you have to escape the dot \. to match it literally.

ONToday@javascript:gs\.beginningOfToday\(\)@javascript:gs\.endOfToday\(\)

var str = 'active=true^opened_by=6816f79cc0a8016401c5a33be04be441^ORassigned_to!=6816f79cc0a8016401c5a33be04be441^short_descriptionISNOTEMPTY^NQopened_atONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()^EQ';
var today = str.replace(/ONToday@javascript:gs\.beginningOfToday\(\)@javascript:gs\.endOfToday\(\)/g, ' is today ');
replacementString = today;
console.log(today);
The fourth bird
  • 154,723
  • 16
  • 55
  • 70
  • 1
    That was exactly what was needed. I had tried with a backslash both sides of the braces e.g. '\(\\)\' which was obviously incorrect. – Alexander Ward Aug 17 '18 at 11:34