regexdomasp-classicrecursive-regex

Does Classic ASP have an object that I can use to browse and modify DOM elements on the server?


I am working in a classic asp application that requires functionality that will modify code that the user copy and pastes into a form. The user is considered a trusted user who is not familiar with html.

I am trying to make it so that if the user wants to change all width="" attributes in the supplied code then all he has to do is fill a textbox label Width with the correct value and press save/submit. Then the script will find all width attributes and update their values in the html snippet that was supplied.

I've been working on a regular expression to do this, but while researching I read that a lot of people do not recommend regexps for this type of thing and would rather use a html parser object of some sort.

Is there an html parser or DOM browser/editor available in classic asp or do I just need to continue my regexp development?

For the regexp, this is what I have so far... still need to modify it to perform replacements on all matches, not just the first one:

function replaceBetween(sSource, sStart, sStop, sValue)

    thisNewValue = sStart & sValue & sStop

    set re = new regexp
    re.pattern = "(" & "" &sStart & ")(.*?)(" & sStop & ")"
    re.IgnoreCase = true

    response.write "Pattern: <b>" & re.pattern & "</b><br />" & vbnewline
    response.write "thisNewValue: <b>" & thisNewValue & "</b><br />" & vbnewline
    response.write "match: <b>" & re.test(sSource) & "</b><br />" & vbnewline

    replaceBetween = re.replace(sSource, thisNewValue)


end function

sourceText = ("<div class='thisclass' id=""thisID""><a anotherthing="""" attribute=""one""><a attribute=""2""><a anotherattribute="" attribute=""three 3""></div>")
replacestart = "attribute="""
replacestop = """"
newvalue = "XXXX"

response.write "updated source: <b>" & server.HTMLEncode(replaceBetween(sourceText,replacestart,replacestop,newvalue)) & "</b><br />" & vbnewline

Solution

  • Is your HTML well formed? If so you could simply use an XML DomDocument. Use XPath to find the attributes you want to replace.

    You can actually use JScript serverside as well in ASP, whicdh might give you access to HTMLDom libraries you could use.