Karan
Karan

Reputation: 3328

javascript - Regex for removing jQuery attribute from <a> hyperlinks

I want to remove the jQuery attribute from the string. Please suggest the regex.

<A href="http://www.yahoo.com" jQuery1327469683587="77" jQuery1327470207412="14">yahoo</A><BR>

Upvotes: 0

Views: 870

Answers (2)

James Jithin
James Jithin

Reputation: 10565

I liked the following way of accomplishing this:

var inputText = "<A jQuery1327469683587=\"77\" href=\"http://www.yahoo.com\" jQuery1327470207412=\"14>\">yahoo</A><BR><A jQuery1327469683587=\"77\" href=\"http://www.yahoo.com\" jQuery1327470207412=\"14>\">yahoo</A><A jQuery1327469683587=\"77\" href=\"http://www.yahoo.com\" jQuery1327470207412=\"14>\">yahoo</A><A jQuery1327469683587=\"77\" href=\"http://www.yahoo.com\" jQuery1327470207412=\"14>\">yahoo</A>";

var matchedTags = inputText.match(/<a[^<>]+[^=]">/gi);     //Match all link tags

alert(matchedTags);
for(i = 0; i < matchedTags.length; i++) {
    var stringBeforeReplace = matchedTags[i];
    matchedTags[i] = matchedTags[i].replace(/\s+jQuery\w+="[^"]*"/g,"");
    inputText = inputText.replace(stringBeforeReplace, matchedTags[i]);
}

alert(inputText);

Upvotes: 1

nnnnnn
nnnnnn

Reputation: 150020

var str = '<A href="http://www.yahoo.com" jQuery1327469683587="77" jQuery1327470207412="14">yahoo</A><BR>'

str = str.replace(/\sjQuery\d+="[^"]*"/g, "")

Or, if the characters after "jQuery" are not all digits:

str = str.replace(/\sjQuery[^=]+="[^"]*"/g, "")

Upvotes: 1

Related Questions