0

I have a below XML file

<configs>
    <config environment="DEV">
        <URL>http://www.devurl.com</URL>
    </config>
    <config environment="SIT">
        <URL>http://www.siturl.com</URL>
    </config>
    <config environment="UAT">
        <URL>http://www.uaturl.com</URL>
    </config>
    <config environment="PROD">
        <URL>http://www.produrl.com</URL>
    </config>
</configs>

and am trying to write a regex to find the URL value that is configured for PROD environment. Below is my sample Regex

 var regexp = /<config environment="PROD">[^]*?<\/config>/gi;
 var matches_array = data.match(regexp);
 console.log(matches_array[0]);

which returns

<config environment="PROD">
    <URL>http://www.produrl.com</URL>
</config>

as a result, but i expect my regex should return URL tag alone as a result for eg :

<URL>http://www.produrl.com</URL> or http://www.produrl.com

Can any one help me to get this work.

Thanks in advance..!

Mohan
  • 3,893
  • 9
  • 33
  • 42
  • You'd be better off with an XML parser.... https://www.npmjs.com/package/xml-parser – Dave Swersky Apr 17 '17 at 01:20
  • I think this is technically a duplicate of http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – snek Apr 17 '17 at 05:40

1 Answers1

0
/(?=config environment="PROD").*\s*<URL>([^<]*)<\/URL>/gm

Demo

I took the prod environment config and extracted the url from it:

  • (?=config environment="PROD") is a Positive Lookahead that checks that we are in the PROD environment configuration
  • .*\s*<URL>...<\/URL> just takes care that we are taking the URL from inside the URL tags
  • ([^<]*) here we have a Capturing Group that extracts the URL.

I hope this helped

Andrei Voicu
  • 740
  • 1
  • 6
  • 13