From what I understand about the JavaScript regex functions, the "replace()" function can take either a string (as you are passing) or a regex pattern.
I *think* what you want to use is:
src = src.replace(/ab/g, "($0)");
The "/g" tells the regex engine to perform a "G"lobal replace which means that it will do the iterations for you. Also the '$0' is a back-reference to the entire matched text - in effect the same as the 'arr[0]' i your program.
I don't know enough about the JavaScript regex functions specifically, but I do know that there is no such thing as a "common" regex in terms of pattern syntax or capabilities of the regex engine. I suspect that the "last index" that you were referring to IS used by some regex implementations to let the user define a starting point (the .NET regex replace() function has a version with 4 parameters, the last one being where to start the search in the text string) and there are all sorts of ways that this is implemented. However the regex engine used in JavaScript seems to be fairly limited in its capabilities (in this regard at least) and therefore you need to use the technique I mentioned above.
Susan