0

i have a xml file to which i need to

<?xml version='1.0' encoding='UTF-8'?><ns0:TradeMessage xmlns:ns0="http://aptp.accenture.com/BuySell">
    <SecuritiesTrade>
        <Direction>BUY</Direction>
        <Other Tags>
        </other Tags>
 </SecuritiesTrade>
</ns0:TradeMessage>

Now what i want is to use a regular expression and remove only this tag:

what regular expression should i use so that the output is :

<?xml version='1.0' encoding='UTF-8'?>
    <SecuritiesTrade>
        <Direction>BUY</Direction>
        <Other Tags>
        </other Tags>
 </SecuritiesTrade>
3
  • 1
    Use proper XML parser instead of regex.. Commented Aug 4, 2015 at 4:06
  • NO i need to use regular expression and replace the tags by null. is there any option i can do that. Also the contents in the <ns.. > may change Commented Aug 4, 2015 at 4:10
  • javascript. i have already used some code to remove few of thing(see below):$XMLStr = $XMLStr.replace(/^<\?xml\s+version\s*=\s*(["'])[^\1]+\1[^?]*\?>/, "").trim(); to remove the start line <?xml version='1.0' encoding='UTF-8'?>. Need the rest to remove the ns tag Commented Aug 4, 2015 at 4:18

1 Answer 1

1

Try this pattern:

<\/?ns0:TradeMessage(?:[^>]*)>

The idea is that we match an open tag:                 <
followed by an optional close slash:                       \/?
followed by ns0:TradeMessage:                             ns0:TradeMessage
followed by any characters other than a close tag: (?:[^>]*)
followed by a close tag:                                          >

var str = "<?xml version='1.0' encoding='UTF-8'?><ns0:TradeMessage\n"+ "xmlns:ns0=\"http://aptp.accenture.com/BuySell\">\n"+
"    <SecuritiesTrade>\n"+
"        <Direction>BUY</Direction>\n"+
"        <Other Tags>\n"+
"        </other Tags>\n"+
" </SecuritiesTrade>\n"+
"</ns0:TradeMessage>";
       
var regex = /<\/?ns0:TradeMessage(?:[^>]*)>/g;

alert(str.replace(regex,""));

Here's a another DEMO

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

1 Comment

Thanks Problem Solved :)

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.