search and replace using regexp

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 & "'."
                'print
                Response.Write(strReturnStr & "<BR>")
            Next
        End Sub
        'call the subroutine
        regExpTest "!moduleStart!([\s\S]+?)!moduleEnd!.", webSiteContent

Open in new window

EliteWebAsked:
Who is Participating?
 
ddrudikConnect With a Mentor Commented:

<%
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

0
 
ddrudikCommented:
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)
    next
    results=Left(results,Len(results)-1) & chr(13)
  next
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)"
0
 
EliteWebAuthor Commented:
Yes but i need the output as :

<% call contactForm(79) %>, so the images is replaced with functions...
0
 
EliteWebAuthor Commented:
Hi,

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

0
 
ddrudikCommented:
Thanks for the question and the points, if there's anything else let me know.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.