var str = "Math.sin(90)"; alert( str = str.replace(/(Math.*?)(\d+)/g, function(a,b,c) { return b + c * Math.PI / 180 }) );