Mr punch
Mr punch

Reputation: 1906

How to replace $1 , $2 in string

var params = ['tom' , 'harry'];

var string = 'hello $1 and $2  how aa are you $1 and $2';

What i tried

var params = ['tom' , 'harry'];
var string = 'hello $1 ,$2  how aa are you $1 , $2';
var temp;
for(var i = 0; i<params.length ; i++)
{
  temp = string.replace(/[$1]+/g,params[i]);   
}

Firefox console wrong output : "hello harry ,harry2 how aa are you harry , harry2"

Final Output : hello tom and harry how are you tom and harry

Upvotes: 1

Views: 2867

Answers (6)

dwi cahyo nugroho
dwi cahyo nugroho

Reputation: 1

I was looking for the same problem and this is what I end up with:

const params = ['tom' , 'harry'];

const string = 'hello $1 and $2  how aa are you $1 and $2';

const result = params.reduce((acc, val, i) => acc.replace(new RegExp(`\\$${i+1}`, 'g'), val), string);

console.log(result);

Upvotes: 0

Muhammad Imran
Muhammad Imran

Reputation: 744

One solution:

string.replace(/\$1/g, params[0]).replace(/\$2/g,params[1])

More explanation:

The reason I put $1 as \$1 because $1, $2,... have special meaning inside regular expressions. They are considered as special characters. E.g., if you want to search . (dot) in your string then you cannot just place . in regex because in regex . means match any character inside string (including dot too); so, in order to find . in your string you've to place(slash \) before dot, like \. , inside regex, so that regex engine can find exact . character.

Upvotes: 5

Mr punch
Mr punch

Reputation: 1906

Replace using SPLIT and JOIN - Don't always need to use .replace

var params = ['tom' , 'harry'];
var string = 'hello $1 ,$2  how aa are you $1 , $2';

for(var i = 0; i<params.length ; i++)
{
  var st = '$' +( i + 1);

  string = string.split(st).join(params[i])
}

OutPut : "hello tom ,harry how aa are you tom , harry"

Upvotes: 2

adam rowe
adam rowe

Reputation: 446

To answer the question, if you're really wanting to replace $1 with tom, you would search for \$1 (notice the escaped $) and replace with tom.

I have a feeling what you really mean is that you've got some text at the positions where $1 and $2 occur. If this is the case, you don't really need to find and replace; you just need to output your variables in those positions.

Upvotes: 0

Mr punch
Mr punch

Reputation: 1906

This is An extinction to @muhammad imran solution with for loop

var params = ['tom' , 'dick', 'harry'];

var string = 'hello $1 ,$2  how $3 aa are you $1 , $2  , $3';

var stringToFire = ''

for(var i = 0 ;i<params.length;i++)
{
  var forDollar = i+ 1;
  var forReplacer =   i;
  stringToFire = stringToFire + '.replace(/\\$dollarNumber/g, params[donkey])'.replace('dollarNumber',forDollar).replace('donkey',forReplacer );
}

var ultimateString = 'string'+stringToFire

eval(ultimateString);

Final Output : hello tom and harry how are you tom and harry

Upvotes: 1

GRS
GRS

Reputation: 13

var params = ['tom', 'harry'];
            var string = "hello $1 en $2";
            string = string.replace("$1", params[0]).replace("$2", params[1]);

Upvotes: 0

Related Questions