JavaScript Search and replace all in javascript/nodejs search replace javascript,location.search.replace javascript,search replace string

My replace all function is as below, it is in commonHelper.js file

exports.replaceAll = function (find, replace, str) {
  return str.replace(new RegExp(find, 'g'), replace);
}

Then I do following

var commonHelper = require('./commonHelper');
var html_body = commonHelper.replaceAll('[[username]]', user_row.username, template_row.message_body);
html_body = commonHelper.replaceAll('[[forgot_pass_link]]', forgot_pass_link, html_body);

this is not properly replacing the [[key]] parts here. What should I change to fix this?

Answer:1

I had to replace special characters. My updated replace all function

exports.replaceAll = function (find, replace, str) {
  var find = find.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
  return str.replace(new RegExp(find, 'g'), replace);
}
Answer:2

There is a replaceAll command in string module.

You might be able to use it like this in util.js:

var S=require('string');
exports.replaceAll=function(hay,rplfrom,rplto)
{
    return S(hay).replaceAll(rplfrom,rplto).s;
};
Answer:3

I'm using a content-script in a chrome extension that I'm writing. I included geolocation in my list of permissions, but on every webpage I still get asked if I want to share my location. I thought ...

I'm using a content-script in a chrome extension that I'm writing. I included geolocation in my list of permissions, but on every webpage I still get asked if I want to share my location. I thought ...

I have a casperJS script which returns JSON when run via commandline. I want to make an API preferably in PHP which runs the script as in commandline (casperJS sample.js) and returns the JSON as ...

I have a casperJS script which returns JSON when run via commandline. I want to make an API preferably in PHP which runs the script as in commandline (casperJS sample.js) and returns the JSON as ...

I'm trying to display category labels on the x-axis on a bar chart but can't work out how to do this. Here's the HTML and JS: <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> &...

I'm trying to display category labels on the x-axis on a bar chart but can't work out how to do this. Here's the HTML and JS: <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> &...

I want to know if there is a way to prevent multiple routes on the same time to instantiate the component if the user is not logged in. for example: angular.module('app', []) .controller('...

I want to know if there is a way to prevent multiple routes on the same time to instantiate the component if the user is not logged in. for example: angular.module('app', []) .controller('...

  1. angular router activate
  2. angular activate router outlet
  3. angular router enabletracing
  4. angular router enable debug
  5. angular activate routing
  6. angular router-outlet activate event
  7. angular router canactivate
  8. angular router link activate
  9. angular router activatedroute
  10. angular routing canactivate
  11. angular 6 router outlet activate event
  12. angular 5 router outlet activate
  13. angular 4 router-outlet activate