Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Help with Regular Expression and Replace for BBCode

Posted on 2007-03-22
9
Medium Priority
?
240 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
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…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

618 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