0

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>
Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
Karan
  • 3,265
  • 9
  • 54
  • 82

2 Answers2

1
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, "")
nnnnnn
  • 147,572
  • 30
  • 200
  • 241
  • can we restrict the replacement to anchor tags only.. I am trying with http://www.gskinner.com/RegExr/ but your regex is not working in it. – Karan Jan 25 '12 at 06:15
  • 1
    It worked fine for me at that site: http://gskinner.com/RegExr/?2vqj1 - note that the `/` at the beginning and the `/g` at the end of the expression in my answer is part of JS regex literal syntax and not part of the regular expression itself, so to test it at that site you have to just use `\sjQuery\d+="[^"]*"` (and set the global option). – nnnnnn Jan 25 '12 at 06:29
  • P.S. As far as restricting it just to anchor tags, if that is the requirement you should update your question, noting that [you can't parse html with regex](http://stackoverflow.com/a/1732454/615754). – nnnnnn Jan 25 '12 at 06:31
  • 1
    You can't really restrict it to anchor tags with regex if there can be more than one "jQuery..." per anchor tag to match. Better off to use a parser, or to write something that looks for the `` tag, works out where it ends, does the replacing within that substring only, and then continues. – mathematical.coffee Jan 25 '12 at 06:38
1

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);
James Jithin
  • 10,183
  • 5
  • 36
  • 51