最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to replace $1 , $2 in string - Stack Overflow

programmeradmin4浏览0评论
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

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

Share Improve this question edited Jul 7, 2015 at 12:30 Mr punch asked Jul 7, 2015 at 12:16 Mr punchMr punch 1,9164 gold badges18 silver badges23 bronze badges 3
  • i am trying this in firefox console – Mr punch Commented Jul 7, 2015 at 12:28
  • yes i have updated tags :) – Mr punch Commented Jul 7, 2015 at 12:31
  • 1 @plutopunch take my code snippet and try it in any browser's console. I tried it in chrome and it works perfectly. – Muhammad Imran Commented Jul 7, 2015 at 12:42
Add a ment  | 

6 Answers 6

Reset to default 5

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.

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"

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

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

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.

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);

发布评论

评论列表(0)

  1. 暂无评论