Recommended: ASP 3.0 Advanced Programming (32) 7.2.4 Client Script Error So far, we have learned about the errors from ASP. However, ASP is also often used to create web pages containing client scripts. If the <SCRIPT> element containing the client code is not set to RUNAT=SERVER
Last time, we talked about how to use regular expression objects to achieve verification of various data in ASP. The article describes the powerful functions of regular expression objects. Next, let's take a look at other functions of regular expression objects. When we surf the Internet, especially when browsing various forums, we often see the word UBB code. What is UBB code? UBB code is a variant of HTML, a special TAG used by Ultimate Bulletin Board (a BBS program abroad, and many places in China use this program). In order to avoid users' unintentional or intentional attacks on the forum by HTML code, various forums basically block HTML code. However, in order to make the forum more colorful, the forum will open some custom Tag tags accordingly, similar to the Tags in HTML, such as [URL],[/URL],[RED],[/RED]. In this way, some functions equivalent to HTML code can be provided, and the forum can be avoided from malicious attacks. These custom Tags are commonly known as UBB code. At present, all fashionable forums support UBB code, such as the forum of Dynamic Technology Network supports UBB code very well. Now let's use regular expression objects to implement our own colorful UBB code.
First of all, your server side (IIS or PWS) must have a VBScript5.0 support environment, which generally means that you can already support regular expression objects after IE5.X is installed.
Regarding common methods and attribute usage methods of regular expression objects, I have already introduced in this article on using regular expression objects to implement data verification in ASP. You can refer to that article. Here, I will mainly introduce the main regular expression object method that implements UBB code: Replace() method.
The syntax of the Replace method is as follows:
The syntax of the Replace method is as follows:
describe
Replace text found in regular expression lookup.
grammar
object.Replace(string1, string2)
The syntax of the Replace method includes the following parts:
Partial description
object Required. Always the name of a RegExp object.
string1 Required. string1 is the string to which text replacement is to be performed.
string2 Required. string2 is a replacement text string.
illustrate
The actual pattern of the replaced text is set through the Pattern property of the RegExp object.
The Replace method returns a copy of string1, where the RegExp.Pattern text has been replaced with string2. If no matching text is found, a copy of the original string1 will be returned.
Next, we want to determine what kind of UBB code we want to implement in the end? The UBB code functions we want to implement this time are as follows:
URL hyperlink
Add a hyperlink to your message, just insert it in the following way (UBB code is bold).
[url]www.test.net[/url]
By inserting it as above, the UBB code will automatically link the URL and ensure that the link is open when the user clicks on a new window.
Email Links
Add an email hyperlink to your message, just follow the following example to insert it (UBB code is bold)
[email][email protected][/email]
Insert as above, UBB code will automatically link to email.
Add to image
Add images to your message, just follow the following example to insert it (UBB code is bold).
[img]http://www.test.net/test.gif[/img]
In the above example, the UBB code will automatically let the image be displayed in your message.
Other TAGs
There are also these tags, u small h1 h2 h3 h4 h5 h6 strike blink sub sup del pre big, the functions of these tags are equivalent to those of html
[h1]This is a title[/h1]
[/tag] is a must
Let a paragraph of text be displayed in color
This is a special tag
[#ff0000]A red word[/#]
#The hexadecimal RGB code of the color followed by, such as red is ff0000, green is 00ff00, and blue is 0000ff
As for the implementation methods of various other UBB codes similar to those above, we will use the above UBB code implementation as an example to explain the powerful functions of regular expression objects.
We mainly use the Replace() method and the corresponding template to easily implement the UBB code function. Here we mainly use two functions written by ourselves, the first function ReplaceTest. The function implemented by this function is mainly to encapsulate the regular expression object and provide three entry parameters:
Patrn
This parameter passes a matching template for UBB code
Str
This parameter passes a string that will be processed in UBB mode, such as the article content.
ReplStr
This parameter passes the HTML code language that matches it.
The exit parameter of the ReplaceTest function is a string replaced by template matching.
The ReplaceTest function code is as follows:
Function ReplaceTest(patrn,str,replStr)
Dim regEx, str1 ' Creates a variable.
str1=trim(str)
Set regEx = New RegExp ' Create regular expression.
regEx.Pattern = patrn ' Set mode.
regEx.IgnoreCase = true ' Set whether it is case sensitive.
ReplaceTest = regEx.Replace(str1, replStr) ' as a replacement.
set regEx=nothing 'Destroy regular expression object
End Function
The second function we want to write is: UBB() function. The function implemented by this function is to convert a piece of text into UBB functions. This function only has one entry parameter:
Str
This parameter passes the string to be processed.
The export parameter of the UBB function is a string processed by UBB code.
The code of the UBB function is as follows: (There are detailed comment information in the program)
Function UBB(str)
dim i,temp ' declare variable
i=1
temp=
do while instr(i,str,[/]>=1 'If the end of the string is not reached
if trim(temp)= then
temp=ReplaceTest((/[i])(/S )(/[/i]),str,<i>$2</i>) 'Document template matching and replacement of UBB code
else
temp=ReplaceTest((/[i])(/S )(/[/i]),temp,<i>$2</i>) 'Document template matching and replacement of UBB code
end if
temp=ReplaceTest((/[b])(/S )(/[/b]),temp,<b>$2</b>) 'Compare template matching and replacement of UBB code
temp=ReplaceTest((/[big])(/S )(/[/big]),temp,<big>$2</big>) 'Document template matching and replacement of UBB code
temp=ReplaceTest((/[strike])(/S )(/[/strike]),temp,<strike>$2</strike>)' perform template matching and replacement of UBB code
temp=ReplaceTest((/[sub])(/S )(/[/sub]),temp,<sub>$2</sub>)' perform template matching and replacement of UBB code
temp=ReplaceTest((/[sup])(/S )(/[/sup]),temp,<sup>$2</sup>)
temp=ReplaceTest((/[pre])(/S )(/[/pre]),temp,<pre>$2</pre>)
temp=ReplaceTest((/[u])(/S )(/[/u]),temp,<u>$2</u>)
temp=ReplaceTest((/[small])(/S )(/[/small]),temp,<small>$2</small>)
temp=ReplaceTest((/[h1])(/S )(/[/h1]),temp,<h1>$2</h1>)
temp=ReplaceTest((/[h2])(/S )(/[/h2]),temp,<h2>$2</h2>)
temp=ReplaceTest((/[h3])(/S )(/[/h3]),temp,<h3>$2</h3>)
temp=ReplaceTest((/[h4])(/S )(/[/h4]),temp,<h4>$2</h4>)
temp=ReplaceTest((/[h5])(/S )(/[/h5]),temp,<h5>$2</h5>)
temp=ReplaceTest((/[h6])(/S )(/[/h6]),temp,<h6>$2</h6>)
temp=ReplaceTest((/[red])(/S )(/[/red]),temp,<font color=red>$2</font>)
'This can add a new UBB code implementation template
temp=ReplaceTest((/[email])(/S )(/[/email]),temp,<a href=mailto:$2 target=_top>$2</a>)
temp=ReplaceTest((/[img])(/S )(/[/img]),temp,<img src=$2>)
temp=ReplaceTest((/[url])(/S )(/[/url]),temp,<a href=$2 target=_top>$2</a>)
temp=ReplaceTest((/[#(/S )])(/S )(/[/#]),temp,<font color=$1>$3</font>)' perform template matching and replacement of UBB code
i=i 1
loop
if trim(temp)<> then
UBB=temp 'passes out the string filtered by UBB code
else
UBB=str 'Pass the string filtered by UBB code
end if
end function
In the code of function UBB, we can see $1, $2, $3, etc. These strings are pronouns for matching strings. Just like some macros, we can use them to replace the matching strings without having to use the program to remember the position of the matching string. Through these two simple function calls, we have implemented the UBB code function. How about it, it's very simple.
In this way, we just need to call the UBB function when displaying the article, process the article content, and then display it on the browser, and realize the UBB code function of the forum. In addition, you can easily add the required UBB code matching replacement template in the function UBB, so that you can continuously expand the functions of your UBB code.
(The above programs are all run and passed in WinNT4.0 English version SP5, IIS4.0, IE5.x)
Share: Asp controls 6 very classic codes for xml database NO.1--Create an XML database data.xml <?xml version=1.0?><records><record><name>caca</name><qq>1