Reputation: 21957
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.
Upvotes: 0
Views: 138
Reputation: 148524
'11111'+(new RegExp("[a-z0-9]*"+lg_name+"[a-z0-9]*",'gi')).exec("Watch out for the rock!")[0]+'22222'
Upvotes: 1
Reputation: 9288
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?"
Upvotes: 1
Reputation: 35822
'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.
Upvotes: 1