Take the 2-minute tour ×

I have the following string:

Are you sure you want to delete "%lg_name%" group?

And in Javascript I have the variable lg_name. How can I replace lg_name string to lg_name variable? This regex shouldn't be related to the variable name. Thanks.

share|improve this question

3 Answers 3

up vote 1 down vote accepted
'Are you sure you "%lg_name%" group?'.replace(/"%[^"%]*%"/, 'value');

Basically, you should find the "%variable_name%" token. You can do that using /"%[^"%]*%"/ pattern. Then you can simply replace it, using the replace method of the your string.

share|improve this answer

Here is your solution, it replace the corresponding %lg_name% part with the variable name lg_name

'Are you sure you "%lg_name%" group?'.replace(/"%([^"%]*)%"/, function ($1,$2){return $2});
>>"Are you sure you lg_name group?"
share|improve this answer
   '11111'+(new RegExp("[a-z0-9]*"+lg_name+"[a-z0-9]*",'gi')).exec("Watch out for the rock!")[0]+'22222'
share|improve this answer

Your Answer


