Solved

Help with Regular Expression and Replace for BBCode

Posted on 2007-03-22
9
223 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

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 information …
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

831 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