Solved

Help with Regular Expression and Replace for BBCode

Posted on 2007-03-22
9
233 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

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 was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

724 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