0

I have an application which generates a random string on the server-side. This string is rendered on page load. I'm able to access the string with JavaScript by selecting it from the DOM element it's contained in.

Assuming the string is in a variable, str, for example:

let str = 'rds$$$IgObk';

I have a template in another part of the page which contains placeholders (e.g. {{code}}). This is accessible in the variable template, e.g.

let template = 'The code is {{code}} ... '; 

What I want to do is replace my placeholder {{code}} with the value of str. So the expected output would be:

The code is rds$$$IgObk

When I do the following:

template = template.replace("{{code}}", str);
console.log(template)

The output is:

The code is rds$$IgObk ...

Note that this is missing one of the $ characters (there are 3 in the original str and there are only 2 in the output).

I understand that certain characters have reserved meanings and this is why it removes one of them when using replace().

The character set I have to generate codes consists of:

  • A-Z (both uppercase and lowercase)
  • Any of these characters !, @, #, and $

Any of these characters can be used in str and there is no limit on the number of times any one character can appear, or the order in which they appear. The length of the generated str is always the same.

All I want to do is use str literally. For example if it contains any special characters then I just want to put those into {{code}}. I've reviewed javascript replace() not replacing text containing literal \r\n strings but don't think this answers my particular question.

For background information: Previously I was doing the string replacement server side using PHP's str_replace(). This does exactly what I want because it replaces the string as-is.

Is there a solution to this in JavaScript that doesn't involve using regular expressions, i.e. a way to specify "use this string as-is"?

Andy
  • 5,142
  • 11
  • 58
  • 131
  • See the duplicate, specifically https://stackoverflow.com/a/59697678/476. – deceze Apr 16 '21 at 08:45
  • @deceze the solution you've linked to won't work in this case. The codes are generated dynamically so I don't know what characters will be in them. I want something that *uses the string as provided* without needing to know which characters to escape/treat differently. If that's not possible then it can't be done. – Andy Apr 16 '21 at 08:59
  • How exactly does using a function as replacement not work? https://jsfiddle.net/qknu61em/ – deceze Apr 16 '21 at 09:02
  • @deceze because what you've put on the jsfiddle isn't the same as any of the given answers in the post you've linked to. You've answered my question in the fiddle (but that isn't given in any of the answers you linked to) so it would probably have been easier just to post that as an answer. Every single "solution" on the linked SO post contains a `$` character. As I've stated in the question, I'm dealing with more than just that character and furthermore don't have a way to pre-determine the number of times/order said characters would appear. – Andy Apr 16 '21 at 09:11
  • [This answer](https://stackoverflow.com/a/59697678/476), which I explicitly linked to above for clarity, contains the exact proposed solution. – deceze Apr 16 '21 at 09:12

0 Answers0