0

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
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
quakkels
  • 11,676
  • 24
  • 92
  • 149
  • You aren't parsing a regular language, so a regular expression is not going to help you. See http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 – Ether Mar 17 '11 at 18:23
  • Agreed. But I have to get this functionality somehow. all I need is to match all cases of `attribute="` somevalue `"` – quakkels Mar 17 '11 at 18:52

1 Answers1

0

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.

James Walford
  • 2,953
  • 1
  • 24
  • 37
  • interesting... I'll test those techniques. – quakkels Mar 17 '11 at 21:10
  • unfortnunately... I guess my html is not "well-formed". I think it's because of lines like this: `` – quakkels Mar 18 '11 at 22:06
  • I guess this puts me back at square one... how do I parse an attribute like `flashvars` above to htmlencode it so that an xml parser will read it so i can parse the attribute? vicious circle – quakkels Mar 18 '11 at 22:08
  • Well you could do string replace on the apersands, they need to be '&' in well formed XML/XHTML. Though you might do better to just preparse with something like HTML Tidy http://www.w3.org/People/Raggett/tidy/ – James Walford Mar 19 '11 at 09:58