Match the regular expression used to obtain HTML tag attributes

Source: Internet
Author: User

Purpose:
1. You want to delete HTML files other than Class, SRC, and href.
For example
1)
<A href = "http://51js.com" Title = "this is the title" class = "A"> title </a>
After the attribute is deleted:
<A href = "http://51js.com" class = "A"> title </a>

2)
<TD style = "color: Red" class = "B" rospan = "3" colspan = "5"> & nbsp; </TD>
After the attribute is deleted:
<TD class = "B" rospan = "3" colspan = "5"> & nbsp; </TD>

I want to find a regular expression that matches such a regular expression. Thank you.

 

LZ, as you can see, first, you want to delete all objects except class, SRC, and href, and second, you want to delete other objects except class, rospan, and colspan. In combination with 1 and 2, you mean to delete the title and style. In this case, you need to retain most tag attributes and delete only a few of them. Then I thought about it. As there are so many tag attributes, it is bound to be very long to match the reserved keyword string. Why don't you raise the limit? Such :(? = Title | style) \ B [^ \ s] + = ["']? [^ "'] * ["']? (? = \ S |> ). Next we will gradually establish and improve this regular expression.

VaR STR = '\
<A href = "http://51js.com" Title = This is the title class = "A"> title </a> \
<TD style = color: Red class = "B" rospan = "3" colspan = "5"> </TD> \
<TD style = "border-Right: # d4d0c8; padding-Right: 0.75pt; border-top: # d4d0c8 ;\
Padding-left: 0.75pt; padding-bottom: 0 cm; border-left: windowtext 0.5pt solid ;\
Width: 62pt; padding-top: 0.75pt; border-bottom: Black 0.5pt solid; Height: 18.75pt ;\
Background-color: Transparent "width = 83 rowspan =" 4 "> \
<A href = "fdsafd" class = "DDD" rowspan = "fdsd"> \
';
STR = Str. Replace (/(? = Title | style) \ B [^ \ s] + = ["']? [^ "'] * ["']? (? = \ S |>)/GI ,'');
Alert (STR)

 

  1. <SCRIPT>
  2. VaR STR = '\
  3. <A href = "http://51js.com" Title = This is the title class = "A"> title </a> \
  4. <TD style = color: Red class = "B" rospan = "3" colspan = "5"> </TD> \
  5. <TD style = "border-Right: # d4d0c8; padding-Right: 0.75pt; border-top: # d4d0c8 ;\
  6. Padding-left: 0.75pt; padding-bottom: 0 cm; border-left: windowtext 0.5pt solid ;\
  7. Width: 62pt; padding-top: 0.75pt; border-bottom: Black 0.5pt solid; Height: 18.75pt ;\
  8. Background-color: Transparent "width = 83 rowspan =" 4 "> \
  9. <A href = "fdsafd" class = "DDD" rowspan = "fdsd"> \
  10. ';
  11. STR = Str. Replace (/(? = Title | style) \ B [^ \ s] + = ["']? [^ "'] * ["']? (? = \ S |>)/GI ,'');
  12. Alert (STR)
  13. </SCRIPT>

Copy the code, run the code, and save the code.

Truncate the source code snippet of the parent and test HTML Tag attribute filtering. To illustrate the problem, filter out the attributes "class" and "Alt" based on the above ".

<Textarea id = "TXT" style = "width: 500px; Height: 500px">

<Div class = "maintable"> <br> <Div class = "subtable nav" style = "width: 100%">
<Span id = "forumlist" onmouseover = "showmenu (this. ID)"> <a href = "index. php"> carefree script </a> </span>
» <A href = "Forumdisplay. php? FID = 1 "> JavaScript & VBScript & DHTML Script Technology Discussion version </a>» find a regular expression to match the HTML tag attributes </div> <br> </div>

<Div class = "maintable">
<Table width = "100%" cellspacing = "0" cellpadding = "0" align = "center" style = "clear: Both;">
<Tr> <TD valign = "bottom">
<Div style = "margin-bottom: 4px">
<A href = "Redirect. php? FID = 1 & tid = 88672 & goto = nextoldset "style =" font-weight: normal "> previous topics </a> | <a href =" Redirect. php? FID = 1 & tid = 88672 & goto = nextnewset "style =" font-weight: normal "> next topic › </a> <br>
</Div>
</TD> <TD width = "40%" align = "right" valign = "bottom">
<Div class = "right"> <a href = "post. php? Action = reply & FID = 1 & tid = 88672 & extra = "> </ a> </div>
<Div id = "newspecialheader" class = "right" onmouseover = "showmenu (this. ID)"> <
Href = "post. php? Action = newthread & FID = 1 & extra ="
> </a> <a href = "###"> </a> </div>
<Div class = "popupmenu_popup newspecialmenu" id = "newspecialheader_menu" style = "display: none">
<Table cellpadding = "4" cellspacing = "0" border = "0" width = "100%">

<Tr> <TD class = "popupmenu_option"> <Div class = "newspecial"> <a href = "post. php? Action = newthread & FID = 1 & extra = & poll = yes "> vote </a> </div> </TD> </tr>
<Div class = "maintable">

</Textarea>

<SCRIPT>
VaR STR = Document. getelementbyid ("TXT"). value;
STR = Str. Replace (/(? = Title | style | class | alt) \ B [^ \ s] + = ["']? [^ "'] * ["']? (? = \ S |>)/GI ,'');
Alert (STR)
</SCRIPT>

  1. <Textarea id = "TXT" style = "width: 500px; Height: 500px">
  2. <Div class = "maintable"> <br> <Div class = "subtable nav" style = "width: 100%">
  3. <Span id = "forumlist" onmouseover = "showmenu (this. ID)"> <a href = "index. php"> carefree script </a> </span>
  4. » <A href = "Forumdisplay. php? FID = 1 "> JavaScript & VBScript & DHTML Script Technology Discussion version </a>» find a regular expression to match the HTML tag attributes </div> <br> </div>
  5. <Div class = "maintable">
  6. <Table width = "100%" cellspacing = "0" cellpadding = "0" align = "center" style = "clear: Both;">
  7. <Tr> <TD valign = "bottom">
  8. <Div style = "margin-bottom: 4px">
  9. <A href = "Redirect. php? FID = 1 & tid = 88672 & goto = nextoldset "style =" font-weight: normal "> previous topics </a> | <a href =" Redirect. php? FID = 1 & tid = 88672 & goto = nextnewset "style =" font-weight: normal "> next topic › </a> <br>
  10. </Div>
  11. </TD> <TD width = "40%" align = "right" valign = "bottom">
  12. <Div class = "right"> <a href = "post. php? Action = reply & FID = 1 & tid = 88672 & extra = "> </ a> </div>
  13. <Div id = "newspecialheader" class = "right" onmouseover = "showmenu (this. ID)"> <
  14. Href = "post. php? Action = newthread & FID = 1 & extra ="
  15. > </a> <a href = "###"> </a> </div>
  16. <Div class = "popupmenu_popup newspecialmenu" id = "newspecialheader_menu" style = "display: none">
  17. <Table cellpadding = "4" cellspacing = "0" border = "0" width = "100%">
  18. <Tr> <TD class = "popupmenu_option"> <Div class = "newspecial"> <a href = "post. php? Action = newthread & FID = 1 & extra = & poll = yes "> vote </a> </div> </TD> </tr>
  19. <Div class = "maintable">
  20. </Textarea>
  21. <SCRIPT>
  22. VaR STR = Document. getelementbyid ("TXT"). value;
  23. STR = Str. Replace (/(? = Title | style | class | alt) \ B [^ \ s] + = ["']? [^ "'] * ["']? (? = \ S |>)/GI ,'');
  24. Alert (STR)
  25. </SCRIPT>

Copy the code, run the code, and save the code.

Improve the above expression to prevent <...> title = "... "</...> delete title = "... ", that is, only the attributes of HTML tags are filtered.

Add logic judgment (?! [^>] * (? = <): The character processing range does not include innertext between tag pairs ==> /(?! [^>] * (? = <))(? = Title | style) \ B [^ \ s] + = ["']? [^ "'] * ["']? (? = \ S |>)/Gi
       

  1. <SCRIPT>
  2. VaR STR = '<a href = "http://51js.com" Title = This is the title class = "A"> Title = This is the title style = color: red </a> <TD style = color: Red class = "B" rospan = "3" colspan = "5"> style = color: red Title = This is the title </TD> ';
  3. STR = Str. Replace (/(?! [^>] * (? = <))(? = Title | style) \ B [^ \ s] + = ["']? [^ "'] * ["']? (? = \ S |>)/GI ,'');
  4. Alert (STR)
  5. </SCRIPT>

Copy the code, run the code, and save the code.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.