-5

I have String of XML nodes (Not the XML File) and I am using below regex which returns the return array. I am looking for a regex which should return the return as a single result string as: 'SUSTED' (SU - from 'abc' node and STED from 'xy' node). Can you please help me with this?

Regex: /<abc>(.*)<\/abc><xy>>(.*)<\/xy>/i
XML String: some junk string not useful so would like to ignore this. param :<root><abc>SU</abc><ab>SDD</ab><xy>STED</xy></root>
some more junk strings, so need to ignore this too...
  • which two nodes? regexp is fine for simple valid non-nested xml, even nested but not repeating-tag is ok. – dandavis Oct 16 '15 at 19:15
  • Just parse the XML (`DOMParser`), and extract the info you want with DOM calls, or `evaluate`. –  Oct 17 '15 at 05:13
  • @torazaburo - No, I can't manipulate it using domParser as my string also have other strings + xml string.. so I am looking for a regex. so theoretically regex should be - Get value between strings(Value1 and Value2) and return the combine result – Swapnil Patil Oct 17 '15 at 05:52
  • I cannot understand what you mean by "my string also have other strings + xml string". Can you add to your question an example of such a string? –  Oct 17 '15 at 06:04
  • hey @torazaburo other string + xml string means: some junk string not useful so would like to ignore this. param :SUSDDSTED some more junk strings, so need to ignore this too – Swapnil Patil Oct 17 '15 at 08:22

1 Answers1

0

Parse the string:

var xml = '<root><abc>SU</abc><ab>SDD</ab><xy>STED</xy></root>';
var parser = new DOMParser();
var doc = parser.parseFromString(xml, "application/xml");

Now do anything you want with the doc:

var extracted = doc.querySelector('abc').textContent + doc.querySelector('xy').textContent;

> "SUSTED"

Do not use regexp to parse XML. You will regret it. XML is not a string; it's a structure that happens to sometimes be represented by a string.

  • I am looking something like by @alan-moore http://stackoverflow.com/questions/869809/combine-regexp but it is matching but it say not capturing group. Live here at https://regex101.com/r/qS7yN9/4 – Swapnil Patil Oct 17 '15 at 10:59
  • Why would you request help with a regexp solution when my entire answer was that that is the wrong solution? If you need to extract some XML then extract it first, then parse it. –  Oct 17 '15 at 12:40
  • This for continuous looking in to this. but I can't use your solution, basically I am receiving a junk for characters from 3rd party and I have to get above nodes + few more details that are not in the xml. If I use your solution that will only do the node value extraction and for other details I have to use other solution.. So, I thought, the regex is the good solution to extract other details + node value extraction. Also, the value that I receive via node are something comes as a plan character. I hope that answers your questions. – Swapnil Patil Oct 17 '15 at 16:05