Home > database >  string replacement with regex
string replacement with regex

Time:10-10

I have field in sql db contains the formula of dynamic calculated question here in my example I have the following string (q1) (q2).

How can I replace q1 and q2 ... qn , to this eval($(q1).val()) eval($(q2).val())

I need code in c# or javascript to do that .

this is my c# code do replacement (q1),(q2),..(qn) with eval($(q1).val()) , but i need to get q dynamically

`string _equation = dr["calc_equation"].ToString();
 _equation = Regex.Replace(_equation, @"[(]q[0-9][)] ", "eval($(q1).val())");`

CodePudding user response:

For example:

const f = '(q1) (q2)';

console.log(f.replace(/\([a-z]\d\)/g,  'eval($$$&.val())'));

Thr first $ escapes the second $, and the $& represents each match.

If there can be more then one letter then use [a-z] .

  • Related