0

I would like to perform a regex match on some XML content in JavaScript.

  1. How do we store XML contents in a JavaScript variable?
  2. What are the effective ways to retrieve the XML content from a JavaScript variable to perform a regex match?
  3. Note: Please suggest code snippets that works in all the browsers.

Sangeeth

Sangeeth Saravanaraj
  • 16,027
  • 21
  • 69
  • 98
  • XML is just a string ? or some other object ? can you include some examples – Manse Nov 20 '11 at 11:24
  • Yes, we can consider the XML content as a string which includes characters like <, >, ", % ., /, \, etc. For example, consider the following XML content should be stored in a javascript variable to perform some regex operations.
    </table>
    – Sangeeth Saravanaraj Nov 20 '11 at 11:27

2 Answers2

1

Regex is generally a bad idea for parsing XML.

Here is a solution using jQuery : XML parsing of a variable string in JavaScript

Community
  • 1
  • 1
pradeek
  • 21,445
  • 2
  • 31
  • 32
0

How do we store XML contents in a JavaScript variable?

That depends on where you are getting the XML from. If you get fetching it over HTTP, then XMLHttpRequest will create an XML object for you (in obj.responseXML). If you are embedded the markup in a string then I believe DOMParser is fairly well supported (and pairs with MSXML.DOMDocument in Microsoft-land)

What are the effective ways to retrieve the XML content from a JavaScript variable

DOM and XPath

to perform a regex match?

I hope you mean "to perform a regex match on text extracted from the XML". Regular expressions are a very poor tool for parsing XML.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335