You can use a regular expression for this, in which you pass the function to String#replace :
s = ; s = s.replace(/([0-9]+)pt/g, function(match, group0) { return Math.round(parseInt(group0, 10) * 96 / 72) + "px"; });
Living example
When you provide a function for the second argument to replace, it is called for each match, with a complete match as the first argument, and then the value of any capture groups for subsequent arguments; as a replacement, the return value is used. So above, I use the capture group to capture the number, and then do the math and return a new line.
You may need or want to tweak the regex a bit to make sure it matches your data (possible spaces between numbers and ones, maybe the i flag to match "PT" as well as "pt", etc.) but what a fundamental approach.
Tj crowder
source share