?
Solved

Help with Regular Expression and Replace for BBCode

Posted on 2007-03-22
9
Medium Priority
?
238 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 1000 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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:   The Exchange of informatio…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

764 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