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>

2 Answers 2

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, "")
Sign up to request clarification or add additional context in comments.

4 Comments

can we restrict the replacement to anchor tags only.. I am trying with gskinner.com/RegExr but your regex is not working in it.
It worked fine for me at that site: 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).
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.
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 <a> tag, works out where it ends, does the replacing within that substring only, and then continues.
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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.