const regex = /([\w]+)[ \t]+=[ \t](Solver[ \t]*\(([\d.]+)[ \t]*,[ \t]*([\d.]+)[ \t]*,[ \t]*([\d.]+)[ \t]*\))\n\r?demo[ \t]*\(\1\.b[ \t]*,[ \t]*\1\.a[ \t]*,[ \t]*\1\.c[ \t]*\)/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('([\\w]+)[ \\t]+=[ \\t](Solver[ \\t]*\\(([\\d.]+)[ \\t]*,[ \\t]*([\\d.]+)[ \\t]*,[ \\t]*([\\d.]+)[ \\t]*\\))\\n\\r?demo[ \\t]*\\(\\1\\.b[ \\t]*,[ \\t]*\\1\\.a[ \\t]*,[ \\t]*\\1\\.c[ \\t]*\\)', 'gm')
const str = `import math
class Solver(object):
def __init__(self, a, b, c):
self.a = a
self.b = b
self.c = c
def demo(b, a, c):
d = b ** 2 - 4 * a * c
if d >= 0:
disc = math.sqrt(d)
root1 = (- b + disc) / (2 * a)
root2 = (- b - disc) / (2 * a)
print(root1, root2)
return root1, root2
else:
raise Exception
s = Solver(2, 123, 0.025)
demo(s.b, s.a, s.c)`;
const subst = `$2\.demo()`;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
console.log('Substitution result: ', result);
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for JavaScript, please visit: https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions