Go Premium for a chance to win a PS4. Enter to Win


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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

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…
As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
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…

885 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