0

Here is my string. Which will contain XML string
Like below

 var str= "<str>rvrv</str>rvrv<q1>vrvv</q1>vrvrv<q2>rtvrvr</q2>";

How can I remove text outside tags(text which does not belong to any tag.) using regular expression. Please help me on this.

2
  • 3
    regular expression is a regular language thus it is not suggested for you to use regex for this. Commented Jan 2, 2012 at 9:33
  • @AlanFoster Then how can I achieve this... Commented Jan 2, 2012 at 9:37

1 Answer 1

3

Assuming your problem is only removing text not enclosed inside an element (and remaining code is well formed so you haven't strings like

var str= "<str>lorem <b>ipsum</str>";

) you could use a regular expression like this

var str= "<str>rvrv</str>rvrv<q1>vrvv</q1>vrvrv<q2>rtvrvr</q2>",
    elements = str.match(/<(.+?)>[^<]+<\/\1>/gi);

console.log(elements.join(''));

and this returns

<str>rvrv</str><q1>vrvv</q1><q2>rtvrvr</q2>

Note: to detect closing tags I used a backreference (see http://www.regular-expressions.info/brackets.html)

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.