Teach you how to replace all HTML tags when outputting text

Source: Internet
Author: User
Tags expression html tags regular expression
HTML markup

Only to the novice, the predecessors do not come to see me move the door to get axe. Hehe
Often see a novice to ask such a topic, in fact, on the regular expression I do not understand.
Yesterday afternoon looked at Microsoft's regular expression, a little understanding.
Today also write a function, only practice, the wrong person please advise.
Here is an example:
The original text is this: "<a href=" "blueidea.com" "><strong>test1</strong></a> <b>test2</b> < Font color=red>test3</font> test4 "
<textarea rows="12" cols="95"><script language= "VBScript" >function regexptest (patrn, strng,rewho) Dim re,match,matches,retstr RetStr= Strng Set Re = New RegExp Re.pattern = patrn Re.ignorecase = True Re.global = True Set matches = Re.execute (RETSTR) For the Match in matches restr = restr & match.value& "," Retstr=replace (retstr,match.value,rewho) Next restr =replace (Restr, "<", "<") Restr=replace (Restr, ">", ">") regexptest = "This is the original:" &strng& "<br> This is replaced by: "&ReStr&" <br> This is the rest: "&retstrend functiondocument.write regexptest (<[^{><}] *>) "," <a href= "" blueidea.com "" ><strong>test1</strong></a> <b>test2</b> <font color=red>test3</font> test4 "," ") </Script></textarea>
[Ctrl + A ALL SELECT hint: You can modify some of the code, and then run]

The key is to <[^{><}]*> this regular expression, although very simple, but I still I understand the shallow, for you to say.
The HTML tag ends with a less than "<", with a greater than number ">".
So all the greater than the less than the number in this regular expression represents the HTML tag, not the special character.
"["] "the square bracket expression contains characters that match only the bracket expression in the regular expression
A single character in the position, I simply understand, it is a match to a position of the individual characters.
The "^" Mark is a special symbol, inside the brackets, followed by a list character, so it means
All characters that are not in the list or range. As I understand it, he's the equivalent of "not" in our program.
So the ^{>&lt in square brackets means that the word Fu Fei is greater than or not less than the number.
Inside the curly braces are removed in fact the same. Curly braces are also special characters
"*" is also a special symbol that matches the previous subexpression 0 or more times,
The general understanding is that in a mark that starts with a less than "" and ends with a greater than sign, and that there can be more than one number of non-or less-than characters. This also matches the HTML tag.
Oh, crap a bunch of

<%
Function Regexptest (patrn,strng,rewho)
Dim re,match,matches
Retstr=strng
Set re = New REGEXP//Set Regular expression
Re. Pattern = PATRN//establish regular expression patterns
Re. IgnoreCase = True//is not case-sensitive, you can close because HTML tags are case-insensitive.
Re. Global = True/is globally available
Set matches = Re. Execute (STRNG)//Perform search
For the match in matches//traversal matching Set
Retstr=replace (retstr,match.value,rewho)//Find the area matching characters and begin the substitution
Next
Regexptest = Retstr
End Function
Content= "<a href= test.asp >test1</a>test2 <strong>test3</strong> test4"//This is the original
pattern= "<[^{><}]*>"//Regular expression pattern, which can also be understood as the character that will be replaced
Restr= ""//what character will be replaced
Response.Write Regexptest (PATTERN,CONTENT,RESTR)
%>

Now there is one more question. "<[^><]*>" since it only matches at the beginning of a less than "",
and a tag that ends with a greater than sign, and there can be more than one number of non-or less-than characters.
So the <a href=<b>> is only matched once, because <a ***> also has HTML tags <b>
To, can not match <a ***&gt, can only match <b> that's all that's left is <a href=>
equals not all of the HTML will be matched ...
I also have a fool's way here. is to execute the Regexptest function two times.


<%
Function Regexptest (patrn,strng,rewho)
Dim re,match,matches
Retstr=strng
Set re = New REGEXP//Set Regular expression
Re. Pattern = PATRN//establish regular expression patterns
Re. IgnoreCase = True//Whether case-insensitive can be closed because HTML tags are case-insensitive
Re. Global = True/is globally available
Set matches = Re. Execute (STRNG)//Perform search
For the match in matches//traversal matching Set
Retstr=replace (retstr,match.value,rewho)//Find the area matching characters and begin the substitution
Next
Regexptest = Retstr
End Function
content= "<a href=<b>>test<>"//This is the original
pattern= "<[^{><}]*>"//Regular expression pattern, which can also be understood as the character that will be replaced
Restr= ""//what character will be replaced
Stest=regexptest (PATTERN,CONTENT,RESTR)
Response.Write Regexptest (PATTERN,STEST,RESTR)
%>


This is possible, but if the original version of the words will certainly affect the speed of the masters are there a better way.

In addition, this function has another use. I'm sure we all know that the Google search keyword is added
Red, and again, you can do it with this function.
For example, I want to "<a href=" "blueidea.com" "><strong>test1</strong></a> <b>test2</b> < The ES in Font color=red>test3</font> test4 all turn red,
That's how the function is called. Regexptest ("Es", Original, "<font Color=red>es</font

> "
Here's a test.
<script language= "VBS" >function regexptest (patrn,strng,rewho) Dim re,match,matches retstr=strng Set re = New REGEXP//Set up regular expression re. Pattern = PATRN//Establish regular expression patterns re. IgnoreCase = True//is not case-sensitive, you can close because HTML tags are case-insensitive. Re. Global = True/is globally available Set matches = Re. Execute (STRNG)//Perform a search for the match in matches//traversal matching set Retstr=replace (retstr,match.value,rewho)//Find the area matching character, and Start replacing Next regexptest = Retstrend functioncontent= "<a href=" "blueidea.com" "><strong>test1</strong> </a> <b>test2</b> <font color=red>test3</font> test4 "//This is the original pattern=" ES "//Regular expression pattern, This can also be understood as the character to be replaced restr= "<font color=red>es</font>"//What character will be replaced document.write regexptest (pattern, CONTENT,RESTR) </script>



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.