Solved

Help with Regular Expression and Replace for BBCode

Posted on 2007-03-22
9
218 Views
Last Modified: 2008-02-01
Building upon:http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/ASP/Q_22467361.html

Unfortunately my Replace does not work.  How would I go about replacing only the matches?
============================================
strExample="<a href=""www.google.com"">Google</a><br><br><a href=""#"" class=""blah blah"" onclick=""blah blah"">Yahoo</a><br><br><a href=""www.msn.com"">MSN</a>"

Function BBCodeURL(str)    
  Set regex = New RegExp
  regex.Global = true
  regex.IgnoreCase = true
  regex.Pattern="<a href=""([^""]*)"">(.+)</a>"
  regEx.Global = True
  BBCodeURL = regEx.Replace(str, "[a href=""$1""]$1[/a]")
End Function
==============================================



0
Comment
Question by:BendOverIGotYourBack
  • 3
  • 2
9 Comments
 
LVL 17

Expert Comment

by:NicksonKoh
ID: 18777710
Hi,

The problem is with regex expression. Try

regex.Pattern="<a href="([^>]+?)">(.+?)<\/a>"

0
 

Author Comment

by:BendOverIGotYourBack
ID: 18777842
Thanks for the reply NiksonKoh.  Gave your suggestion a try, but that returns

[a href="www.google.com"]www.google.com[/a]

Was hoping to return

[a href="www.google.com"]Google[/a][a href="www.msn.com"]MSN[/a]
0
 
LVL 84

Expert Comment

by:ozo
ID: 18777932
regEx.Replace(str, "[a href=""$1""]$2[/a]")
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 17

Expert Comment

by:NicksonKoh
ID: 18777983

You will need to loop the matches as follows

Function BBCodeURL(str)    
  Set regex = New RegExp
  regex.Global = true
  regex.IgnoreCase = true
  regex.Pattern="<a href="([^>]+?)">(.+?)<\/a>"
  regEx.Global = True
  Set matches = regEx.Execute(str)
  For each m in matches
    BBCodeURL = BBCodeURL & "[a href=""" & m.Submatches(0) & """]" & m.Submatches(1) & "[/a]")
  Next
End Function

I am unable to test the above code and can't remember if submatches start from 0. But you should get the idea.

cheers
Nickson
0
 

Author Comment

by:BendOverIGotYourBack
ID: 18782281
Thanks Nickson.  Gave it a shot and if I try to run the loop, I get the following error below.  If I take out the loop, no error.  Not sure why it's declaring it an invalid character.

Microsoft VBScript compilation  error '800a0408'
Invalid character
line 8
regex.Pattern="<a href="([^>]+?)">(.+?)<\/a>"
-------------------------------------^
0
 
LVL 17

Accepted Solution

by:
NicksonKoh earned 250 total points
ID: 18790039
Hi,

The pattern should be

regex.Pattern="<a href=""([^>]+?)"">(.+?)<\/a>"

The double quotes have to be replaced with 2 double quotes.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now