search and replace using regexp

Posted on 2008-10-12
Last Modified: 2012-05-05
Hi i need some help on this one, i use a wysiwyg editor, but the editor is not able to show <% %> tags in wysiwyg, so insted i insert an image containg an ALT text.

e.g.: <img alt="!moduleStart!contactform(79)!moduleEnd!" src="/admin/editor/scripts/module.gif" />

So when i post the data to my database i want to replace this tag with:

<% call contactForm(79) %>

I worked out a solution with a simple replace function but the hurdle is i can have multiple images in the wysiwyg editor and theese were not replaced.

I tried to worksomething out look the code snippet, but i cant figure it out...

webSiteContent = Request.Form("inpContent")
        'this sub finds the matches
        Sub RegExpTest(strMatchPattern, strPhrase)
            'create variables
            Dim objRegEx, Match, Matches, StrReturnStr
            'create instance of RegExp object
            Set objRegEx = New RegExp
            'find all matches
            objRegEx.Global = True
            'set case insensitive
            objRegEx.IgnoreCase = True
            'set the pattern
            objRegEx.Pattern = strMatchPattern
            'create the collection of matches
            Set Matches = objRegEx.Execute(strPhrase)
            'print out all matches
            For Each Match in Matches
                strReturnStr = "Match found at position "
                strReturnStr = strReturnStr & Match.FirstIndex & "." & Match
                'Value is '"
                strReturnStr = strReturnStr & Match.value & "'."
                Response.Write(strReturnStr & "<BR>")
        End Sub
        'call the subroutine
        regExpTest "!moduleStart!([\s\S]+?)!moduleEnd!.", webSiteContent

Open in new window

Question by:EliteWeb
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
LVL 27

Expert Comment

ID: 22697061
With the content:
<img alt="!moduleStart!contactform(79)!moduleEnd!" src="/admin/editor/scripts/module.gif" />
<img alt="!moduleStart!contactform(80)!moduleEnd!" src="/admin/editor/scripts/module.gif" />
<img alt="!moduleStart!contactform(81)!moduleEnd!" src="/admin/editor/scripts/module.gif" />
<img alt="!moduleStart!contactform(82)!moduleEnd!" src="/admin/editor/scripts/module.gif" />

And the ASP Code:
Set regEx = New RegExp
regEx.Global = True
sourcestring = "your source string"
regEx.Pattern = "!moduleStart!([\s\S]+?)!moduleEnd!."
Set Matches = regEx.Execute(sourcestring)
  For z = 0 to Matches.Count-1
    results = results & "Matches(" & z & ") = " & chr(34) & Server.HTMLEncode(Matches(z)) & chr(34) & chr(13)
    For zz = 0 to Matches(z).SubMatches.Count-1
      results = results & "Matches(" & z & ").SubMatches(" & zz & ") = " & chr(34) & Server.HTMLEncode(Matches(z).SubMatches(zz)) & chr(34) & chr(13)
    results=Left(results,Len(results)-1) & chr(13)
Response.Write "<pre>" & results

I get:
Matches(0) = "!moduleStart!contactform(79)!moduleEnd!""
Matches(0).SubMatches(0) = "contactform(79)"
Matches(1) = "!moduleStart!contactform(80)!moduleEnd!""
Matches(1).SubMatches(0) = "contactform(80)"
Matches(2) = "!moduleStart!contactform(81)!moduleEnd!""
Matches(2).SubMatches(0) = "contactform(81)"
Matches(3) = "!moduleStart!contactform(82)!moduleEnd!""
Matches(3).SubMatches(0) = "contactform(82)"

Author Comment

ID: 22697083
Yes but i need the output as :

<% call contactForm(79) %>, so the images is replaced with functions...
LVL 27

Accepted Solution

ddrudik earned 125 total points
ID: 22697891

Set regEx = New RegExp
regEx.Global = True
sourcestring = "your source string"
regEx.Pattern = "<img alt=""!moduleStart!([\s\S]+?)!moduleEnd!""[^>]*>"
replacementstring = "<% call $1 %>"
Response.Write regEx.Replace(sourcestring,replacementstring)

Open in new window


Author Comment

ID: 22698025

Not 100% :-)

Microsoft VBScript compilation error '800a0409'

Unterminated string constant

/admin/editor/loadEditor.asp, line 44

replacementstring = "<% call $1

But this works.

	Set regEx = New RegExp
		regEx.Global = True
		sourcestring = sNewHTML
		regEx.Pattern = "<img alt=""!moduleStart!([\s\S]+?)!moduleEnd!""[^>]*>"
		replacementstring = "<% call $1 %" & ">"
		sNewHTML = regEx.Replace(sourcestring,replacementstring)

Open in new window

LVL 27

Expert Comment

ID: 22698644
Thanks for the question and the points, if there's anything else let me know.

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

by Batuhan Cetin Regular expression is a language that we use to edit a string or retrieve sub-strings that meets specific rules from a text. A regular expression can be applied to a set of string variables. There are many RegEx engines for u…
Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

735 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