Reputation: 697
Why doesn't this work?
var str = "nd2(31)jnd";
str = str.replace(/[0-9](/,/[0-9]*(/);
I want to replace every number with a paranthese in front ex: "2(" with "2*("
Upvotes: 0
Views: 48
Reputation: 15552
Your regex is wrong, this does what you want:
str.replace(/([0-9]+)\(/g, "$1*(");
$1
references the first match in parens ()
, and you have to escape \(
to match it.
Update: add g
for global matching
2(3(4+5)) => 2*(3*(4+5))
Update: Making it work on the other end of the parens too, combined:
str.replace(/(\d+)\(/g, "$1*(").replace(/\)(\d+)/g, ")*$1");
Upvotes: 2