Using "Regular Expression" objects in ASP to implement UBB-style forums

Source: Internet
Author: User
Tags exit copy expression functions implement net variable trim
Ubb| Object | regular Last time, we talked about how to use the "regular expression" object to validate various data in ASP, the powerful function of regular expression object is described in this article, and then we look at the other features of the regular Expression object. When we surf the internet, especially when browsing various forums, we often meet the word "UBB code". What is UBB code? UBB code is a variant of HTML http://www.alixixi.com/program/a/, is Ultimate Bulletin Board (a foreign BBS program, there are a lot of local use of this program) using a special tag. In order to avoid the user unintentional or intentional HTML code to attack the forum, the various forums basically blocked the HTML code, but in order to make the forum more "colorful", the forum will be appropriate to open some of the custom tag tag, similar to the HTML tag, such as "http:// Www.alixixi.com/program/a/,http://www.alixixi.com/program/a/,[red]http://www.alixixi.com/program/a/,[/red] ", so , you can provide some functions equivalent to HTML code, and avoid the forum by malicious attacks, these custom tags are commonly known as UBB code. Today's trendy forums support UBB code, such as: http://www. Chinaasp. COM (Dynamic Technology Network) of the forum is very good support UBB code. Now, let's use the regular expression object together to implement our own colorful UBB code.

First of all, your server side (IIS or PWS) to have a VBScript5.0 support environment, generally refers to the installation of ie5.x can already support the "regular expression" object.

Common methods about regular expression objects, the use of the property, I in the "ASP" in the use of "regular expression" object to achieve data validation has been introduced in detail, you can see that article, here, I mainly introduce the implementation of "UBB code" The primary regular Expression object method: The Replace () method.

The Replace method syntax is as follows:

The Replace method syntax is as follows:

Describe

Replaces the text found in the regular expression lookup.

Grammar

Object. Replace (string1http://www.alixixi.com/program/a/, string2)

The syntax for the Replace method consists of the following sections:

Partial description

Required for object. Always the name of a RegExp object.

String1 is required. String1 is the string that will be replaced with text.

String2 is required. String2 is a replacement text string.

 

Description

The actual mode of the replaced text is set by the RegExp object's Pattern property.

The Replace method returns a copy of the string1, where the Regexp.pattern text has been replaced with string2. If no matching text is found, a copy of the original string1 is returned.

Next, we're going to make sure what kind of UBB code do we ultimately want to implement? This time we want to implement the UBB code features as follows:

URL Super Link
Add a hyperlink to your message as long as it is nested in the following ways (UBB code is bold).

Http://www.alixixi.com/program/a/www.test.net

In the above example, the UBB code will automatically link to the URL and ensure that the link is open when the user clicks on the new window.

Email links
Add an email hyperlink to your message, just follow the example set (UBB code is bold)

Uestc95@263.net

In the above example, the UBB code automatically generates a link to the e-mail message.

Add image
Add an image to your message, just press the example to set it in (UBB code is bold).

In the example above, the UBB code automatically makes the image appear in your message.

Other tag

And these tag http://www.alixixi.com/program/a/, u small H1 H2 H3 h4 h5 h6 strike Blink sub sup del pre big HTTP://WWW.ALIXIXI.C om/program/a/, these tag functions are equivalent to HTML functionality.

[H1] This is a title [/H1]

[/tag] is a must

Make a paragraph of text appear with color
This is a special tag

[#ff0000] a red word [/#]

#后面跟的颜色的16进制RGB代码, such as Red is ff0000http://www.alixixi.com/program/a/, Green is 00ff00http://www.alixixi.com/program/a/, and Blue is 0000ff.

As with all the other UBB code implementations similar to the above, we use the above UBB code implementation as an example to explain the "regular expression" object's powerful features.

We mainly use the Replace () method with the corresponding template, can easily realize UBB code function, where we mainly used two of their own writing functions, the first function replacetest, this function is to implement the function of the "regular expression" object is encapsulated, Provides three entry parameters:

Patrn
This parameter passes a matching template for the UBB code.

Str
This parameter passes a string that will be processed in a UBB way, such as the content of the article.

Replstr
This parameter passes a matching HTML code language.

The exit parameter of the Replacetest function is the string after the template matching replacement.

The Replacetest function code is as follows:

Function Replacetest (PATRNHTTP://WWW.ALIXIXI.COM/PROGRAM/A/,STRHTTP://WWW.ALIXIXI.COM/PROGRAM/A/,REPLSTR)

Dim regexhttp://www.alixixi.com/program/a/, str1 ' Set variable.

Str1=trim (str)

Set regEx = New RegExp ' establishes a regular expression.

Regex.pattern = Patrn ' Set mode.

Regex.ignorecase = True ' sets whether case sensitive.

Replacetest = Regex.Replace (str1http://www.alixixi.com/program/a/, Replstr) ' for replacement.

Set regex=nothing ' destroys regular expression objects

End Function

The second function we want to write is: UBB () function. This function is the function of the implementation of a piece of text Ubb function conversion. This function has only one entry parameter:

Str
This parameter passes the string to be processed.

The exit parameter of the UBB function is a string that is processed after UBB code.

The code for the UBB function is as follows: (Detailed comment information in the program)

Function UBB (str)

Dim ihttp://www.alixixi.com/program/a/,temp ' Declare variable

I=1

Temp= ""

Do While InStr (ihttp://www.alixixi.com/program/a/,strhttp://www.alixixi.com/program/a/, "[/]>=1 ') if the end of the string is not reached

If trim (temp) = "" Then

Temp=replacetest ("(\)" (\s+) (\) http://www.alixixi.com/program/a/,strhttp://www.alixixi.com/program/a/, "<i>$2</i>") ' to match and replace the template for UBB code

Else

Temp=replacetest ("(\)" (\s+) (\) http://www.alixixi.com/program/a/,temphttp://www.alixixi.com/program/a/ , "<i>$2</i>") ' to match and replace the template for UBB code

End If

Temp=replacetest ("(\)" (\s+) (\) http://www.alixixi.com/program/a/,temphttp://www.alixixi.com/program/a/ , "<b>$2</b>") ' to match and replace the template for UBB code

Temp=replacetest ("(\[big]) (\s+) (\[/big)" http://www.alixixi.com/program/a/,temphttp://www.alixixi.com/program/ A/, "<big>$2</big>") ' to match and replace the template for UBB code

Temp=replacetest ("(\[strike]) (\s+) (\[/strike)" http://www.alixixi.com/program/a/,temphttp://www.alixixi.com/ program/a/, "<strike>$2</strike>") ' for template matching and replacement of UBB code

Temp=replacetest ("(\[sub]) (\s+) (\[/sub)" http://www.alixixi.com/program/a/,temphttp://www.alixixi.com/program/ A/, "<sub>$2</sub>") ' to match and replace the template for UBB code

Temp=replacetest ("(\[sup]) (\s+) (\[/sup)" http://www.alixixi.com/program/a/,temphttp://www.alixixi.com/program/ A/, "<sup>$2</sup>")

Temp=replacetest ("(\[pre]) (\s+) (\[/pre)" http://www.alixixi.com/program/a/,temphttp://www.alixixi.com/program/ A/, "<pre>$2</pre>")

Temp=replacetest ("(\)" (\s+) (\) http://www.alixixi.com/program/a/,temphttp://www.alixixi.com/program/a/ , "<u>$2</u>")

Temp=replacetest ("(\[small]) (\s+) (\[/small)" http://www.alixixi.com/program/a/,temphttp://www.alixixi.com/ program/a/, "<small>$2</small>")

Temp=replacetest ("(\[h1]) (\s+) (\[/H1)" http://www.alixixi.com/program/a/,temphttp://www.alixixi.com/program/a/ , "

Temp=replacetest ("(\[h2]) (\s+) (\[/H2)" http://www.alixixi.com/program/a/,temphttp://www.alixixi.com/program/a/ , "

Temp=replacetest ("(\[h3]) (\s+) (\[/H3)" http://www.alixixi.com/program/a/,temphttp://www.alixixi.com/program/a/ , "

Temp=replacetest ("(\[h4]) (\s+) (\[/H4)" http://www.alixixi.com/program/a/,temphttp://www.alixixi.com/program/a/ , "

Temp=replacetest ("(\[h5]) (\s+) (\[/H5)" http://www.alixixi.com/program/a/,temphttp://www.alixixi.com/program/a/ , "

Temp=replacetest ("(\[h6]) (\s+) (\[/H6)" http://www.alixixi.com/program/a/,temphttp://www.alixixi.com/program/a/ , "

Temp=replacetest ("(\[red]) (\s+) (\[/red)" http://www.alixixi.com/program/a/,temphttp://www.alixixi.com/program/ A/, "<font color=red>$2</font>")

' Here you can add a new UBB code implementation template

Temp=replacetest ("(\)" (\s+) (\) "http://www.alixixi.com/program/a/,temphttp://www.alixixi.com/program/a/," <a href= "" Mailto:$2 "" target=_top>$2</a> ")

Temp=replacetest ("(\)" http://www.alixixi.com/program/a/,temphttp://www.alixixi.com/program/a/, "

Temp=replacetest ("(\)" (\s+) (\) "http://www.alixixi.com/program/a/,temphttp://www.alixixi.com/program/a/," <a Href= "" $ "" target=_top>$2</a> ")

Temp=replacetest ("(\[# (\s+))" (\s+) (\[/#)) "Http://www.alixixi.com/program/a/,temphttp://www.alixixi.com/program /a/, "<font color=$1>$3</font>") ' for template matching and replacement of UBB code

I=i+1

Loop

If Trim (temp) <> "" Then

Ubb=temp ' will pass the string filtered by UBB code

Else

Ubb=str ' will pass the string filtered by UBB code

End If

End Function


In the code of the function UBB we can see "$", "$", "$" and so on, these strings are the pronouns of the matching strings, just like some "macros", we can use them to replace the matching string without having to use the program to remember where the string matches. Through these two simple function calls, we realize the function of UBB code, how, very simple.

In this way, we simply call the UBB function when the article is displayed, after the content of the article is processed, and then displayed to the browser above, it realizes the forum's UBB code function. Also, you can easily add the required UBB code matching replacement template to the function UBB, allowing you to continually expand the functionality of your UBB code.

(The above procedures are in the WinNT4.0 English version sp5,iis4.0http://www.alixixi.com/program/a/,ie5.x run through)

Related Article

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.