When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles [1.x] [2.0]
ASPFAQs.com
Message Board
Related Web Technologies
User Tips!
Coding Tips
Search

Sections:
Book Reviews
Sample Chapters
Commonly Asked Message Board Questions
Headlines from ASPWire.com
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Security
Stump the SQL Guru!
Web Hosts
XML Info
Information:
Advertise
Feedback
Author an Article
Technology Jobs

















internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers
ASP ASP.NET ASP FAQs Message Board Feedback ASP Jobs
Print this page.

Windows Systems Administrator
Jupitermedia
US-CT-Darien

Justtechjobs.com Post A Job | Post A Resume

Published: Monday, December 04, 2000

Common Applications of Regular Expressions, Part 4
By Richard Lowe


  • Read Part 1
  • Read Part 2
  • Read Part 3

  • In Part 3 we looked at parsing data files with regular expressions. In this final part, we'll look at how to use regular expressions to perform powerful string replacements!

    - continued -

    Replacing Values
    The final example we'll look at is the replace functionality of regular expressions in VBScript. ASP often is called to dynamically apply formatting to the text it retrieves from the numerous data sources it has available to it. One handy feature of VBScript's regular expressions allow it to modify the complex patterns it matches. A prime example of this is the highlighting of certain words, such as keywords in a search by adding HTML tags.

    To illustrate this, here is an example of highlighting all references to ".NET" in a string. This string could easily come from any source, such as a database or another web site.

    <%
      Set regEx = New RegExp
      regEx.Global = true
      regEx.IgnoreCase = True
    
      ' Pattern finds any word (or URL) with
      ' .NET on the end of it
      regEx.Pattern = "(\b[a-zA-Z\._]+?\.NET\b)"
    
      ' Input a string to test our replace functionality:  
      strText = ""
      strText = strText & "Microsoft has launched a new Web site," & _
                          " www.ASP.NET. " & vbCrLf & "This Web site (as you " & _
                          "can probably guess) contains information " & vbCrLf
      strText = strText & "on the next ""version"" of ASP: ASP.NET. The site " & _
                          "contains links to " & vbCrLf & "download the latest " & _
                          "ASP.NET bits, contains a FAQ section, links " & vbCrLf
      strText = strText & "to ASP community sites, and other great information. " & _
                          "To learn more " & vbCrLf & "about ASP.NET, check out " & _
                          "www.ASP.NET and the 4Guys ASP.NET " & vbCrLf
      strText = strText & "Article Index! " & vbCrLf
    
      ' Call Replace method of the regular expression: 
      ' the $1 means place the matching text here
      Response.Write regEx.Replace(strText, _
                     "<b style='color: #000099; font-size: 18pt'>$1</b>")
    %>
    
    [View the live demo!]

    In this example, there are several important things to note. The whole regular expression is enclosed in parenthesis - (). This tells it to capture any pattern it matches for use later. This captured match is then referenced in the replacement text by $1. Up to nine such captures can be done per match, referenced by $1 to $9. The Replace method of the regular expression object is different from the Replace global function in VBScript. It takes only two parameters, the string to search in and the replacement text.

    Also in the example, a bold tag, with other style attributes was added to surround matching text for emphasis on the ".NET" matches. A search and replace like this could easily highlight search terms from a search of your site, or place automatic links to other pages on keywords that appear in any text.

    Conclusion
    I hope this exploration of some of the uses of regular expressions have given you ideas about how and where to use them in your web applications. Regular expressions are a very powerful way to extend the functionality of VBScript and JScript's string handling. Visual Basic applications, as well, can benefit from regular expressions. Microsoft has made the VBScript and JScript RegExp objects available to VB (or any COM-aware language). Just select Microsoft VBScript Regular Expressions under Project --> References and the RegExp object is available to extend your VB application's string handling, as well.

    There is also a bright future for regular expressions in ASP.NET, as they provide the mechanism for one of the major forms of server control form validation, and are exposed throughout the whole .NET framework in the System.Text.RegularExpressions namespace! (To learn more about ASP.NET, be sure to check out the ASP.NET Article Index Happy Programming!

    Richard Lowe works as a Development Services Consultant for Spherion's Technology Business Solutions Group...


    Windows Internet Technology | ASP.NET [1.x] [2.0] | ASPMessageboard.com | ASPFAQs.com | Advertise | Feedback | Author an Article



    JupiterOnlineMedia

    internet.comearthweb.comDevx.commediabistro.comGraphics.com

    Search:

    Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

    Jupitermedia Corporate Info


    Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

    Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

    Solutions
    Whitepapers and eBooks
    Microsoft Article: HyperV-The Killer Feature in WinServer ‘08
    Avaya Article: How to Feed Data into the Avaya Event Processor
    Microsoft Article: Install What You Need with Win Server ‘08
    HP eBook: Putting the Green into IT
    Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
    Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
    Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
    Avaya Article: Setting Up a SIP A/S Development Environment
    IBM Article: How Cool Is Your Data Center?
    Microsoft Article: Managing Virtual Machines with Microsoft System Center
    HP eBook: Storage Networking , Part 1
    Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
    MORE WHITEPAPERS, EBOOKS, AND ARTICLES
    Webcasts
    Intel Video: Are Multi-core Processors Here to Stay?
    On-Demand Webcast: Five Virtualization Trends to Watch
    HP Video: Page Cost Calculator
    Intel Video: APIs for Parallel Programming
    HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
    Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
    MORE WEBCASTS, PODCASTS, AND VIDEOS
    Downloads and eKits
    Sun Download: Solaris 8 Migration Assistant
    Sybase Download: SQL Anywhere Developer Edition
    Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
    Red Gate Download: SQL Compare Pro 6
    Iron Speed Designer Application Generator
    MORE DOWNLOADS, EKITS, AND FREE TRIALS
    Tutorials and Demos
    How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
    eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
    IBM Article: Collaborating in the High-Performance Workplace
    HP Demo: StorageWorks EVA4400
    Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
    Microsoft How-to Article: Get Going with Silverlight and Windows Live
    MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES