View Full Version : Regular Expression Problem

September 27th, 2010, 04:12 PM
I want to replace ampersands with the corresponding HTML code in my data. When I try to use the following regular expression:
return data.replace(/&(\w+);/g, "&$1");
it works great for:
var data = M&M; //returns M&M
but including a parenthesis alters my output:
var data = M&M(R); //returns M
I tried swapping the \w with a \S but that yielded the same result. Can anyone suggest how I need to modify my expression so that the latter value correctly returns as:
// M&M(R)
(I considered using NormalizeEntities(), but that converts the embedded tags to HTML code as well which is a problem.)

September 28th, 2010, 06:13 AM
return data.replace(/&(\S+);/g, "&$1");
seems to work. I thought I tried that last night, but I must have been tired...