Regular expression matching replacement problem?

string 1
test ()
to replace with->
print ("test ()")

string 2
test (a, b)
to be replaced by->
print ("test (a, b)", a, b)

how to write matching and replacing regularities, can they be implemented with regular substitutions?

^(test\((.*)\))$
->
print\("\1"\, \2\)

if I write the string 1 like this, I can"t replace..

correctly.
Mar.11,2022

/^(test\((.*)*\))$/

var str = "test(p1,p2)";
var reg = /^test\((.*)\)$/;
var changeStr = str.replace(reg,function(match,$1){ return $1?`print("${match}",${$1})`:`print("${match}")`});
console.log(changeStr);

clipboard.png






Menu