views:

42

answers:

1

It's much easier to show the Javascript code than offer a complex explanation:

var html = '<input tabindex="1" type="text">';
html.replace(/tabindex="([0-9]+)"/g, 'tabindex="' + ("$1" * i) + '"');

In this code, "$1" will always be a string, meaning this RegExp will always result in 'tabindex="NaN"' ... Using parseInt doesn't work.

Is it possible to cast the response from the RegExp as an integer so I can perform maths on the replacement?

+4  A: 

The replacement argument can be a function:

html.replace(/tabindex="([0-9]+)"/g, function (all, tabindex) {
        return 'tabindex="' + (tabindex * i) + '"'
    });

However, you should only be using regexps to manipulate HTML if you don't have a parser. If this code is to run in a browser, then you have a parser. If the node is coming from another document, use document.importNode. If the node is in the same document, use Node.cloneNode. Both support deep copying.

outis
You sir are a scholar and a gent. Many thanks.
beseku