2

I have a problem with double quotes in classic ASP.

I want to replace double quotes " in a string. The string contains xml and I don't want to replace double quotes (for attributes) inside tags.

So if I wanted to replace double quotes with single quotes, I'd want my string to go from this:

<MyDinosaurDocument DocType="Brachysaurus">"Hello" said the little dinosaur</MyDinosaurDocument>

to this:

<MyDinosaurDocument DocType="Brachysaurus">'Hello' said the little dinosaur</MyDinosaurDocument>

I've tried using regular expressions and would like to fix this problem with them -- but I'm sadly out of my depth.

All and any help is greatly appreciated.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
adrianos
  • 1,501
  • 2
  • 18
  • 22

3 Answers3

4

I wouldn't use Regex to solve this problem. Here is a simple chunk of code that would do it:-

Dim dom : Set dom = CreateObject("MSXML2.DOMDocument.3.0")

dom.LoadXml myXMLString

Dim node
For Each node in dom.SelectNodes("//*/text()")
    node.nodeValue = Replace(node.nodeValue, """", "'")
Next

myXMLString = dom.xml

Of course you probably at some point need to load the XML into a DOM anyway so once that is done there is no need to read the string back out.

AnthonyWJones
  • 187,081
  • 35
  • 232
  • 306
0

Use &quot; to escape quotes in XML.

Gerrie Schenck
  • 22,148
  • 20
  • 68
  • 95
0

As always when dealing with non-regular data, the answer is not to use a regular expression. Really, don’t. XML and HTML should always be parsed by appropriate parsers and furthermore ASP gives you the means to do this easily. Using regular expressions here is a serious security liability.

Community
  • 1
  • 1
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214