search and replace using regexp

Posted on 2008-10-12
Medium Priority
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
  • 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 500 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

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…
We are witnesses that everyone is saying that our children shouldn't "play" with a technology because it is dangerous. This article is going to prove that they are wrong.
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…

601 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