-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...

5
  • which two nodes? regexp is fine for simple valid non-nested xml, even nested but not repeating-tag is ok. Commented Oct 16, 2015 at 19:15
  • Just parse the XML (DOMParser), and extract the info you want with DOM calls, or evaluate. Commented Oct 17, 2015 at 5: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(<abc>Value1</abc> and <xy>Value2</xy>) and return the combine result Commented Oct 17, 2015 at 5: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? Commented Oct 17, 2015 at 6:04
  • hey @torazaburo other string + xml string means: 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 Commented Oct 17, 2015 at 8:22

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I am looking something like by @alan-moore stackoverflow.com/questions/869809/combine-regexp but it is matching but it say not capturing group. Live here at regex101.com/r/qS7yN9/4
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.
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.