Replace() function adding extra parenthesis to string values that have parenthesis.
I'm attempting to add text to string values in an array. Some of these strings have parenthesis around them and for some reason the replace() function is adding extra parenthesis around these.
So "apples" becomes "I like apples." but "(oranges)" becomes "(I like (oranges).)"
Why is this happening? Is there a better way to do this?
Here is my code:
var words = new Array();
words = ["apples", "(oranges)", "peaches", "(mangos)"];
for (i = 0; i < words.length; i++) {
words[i] = words[i].replace(new RegExp(words[i], "g"), "I like " + words[i] + ".");
alert(words[i]);
}
