p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Javascript (http://p2p.wrox.com/forumdisplay.php?f=85)
-   -   Regular expression help!!! (http://p2p.wrox.com/showthread.php?t=16983)

alex_read August 17th, 2004 09:48 AM

Regular expression help!!!
 
<tag><tag>123@#?</tag>

Could anyone please give me a regular expression syntax which will pick up absolutely anything which could appear between <tag> and </tag> please>???

I'm trying to use this, but it picks up the entire string (i.e. the first instance of <tag>, rather than just the "<tag>123@#?</tag>" string...

tag>([\d\D\s\S\w\W]+)</tag

Thanks!!!


ChrisScott August 17th, 2004 10:24 AM

Hi Alex,

Try...

Code:

tag>([^<]+)</tag
HTH,

Chris


alex_read August 17th, 2004 11:01 AM

Hi Chris!

Thanks for the reply! :D Unfortunately, my real life example has some processing instructions in there - the string actually looks more like this:

<tag><tag><tag_2>1</tag_2>2<?instruction1?>3@#?</tag>

I kind of simplified it a bit too much when doing the first post, sorry! What I'm after is to look for everything between the 2 <tag> um, tags within that string.

You're code would have worked great, but for it having the extra <tag_2> and <? instuction tags in there. Thanks again!


ChrisScott August 18th, 2004 02:50 AM

Hi Alex,

In that case you'll need something like:

Code:

<tag>((?:[^<]*|<(?:[^t]|t[^a]|ta[^g]|tag[^\s>]+)[^>]*>)*)</tag>
Bit of a pain to construct for different tags though, so the following function should be of use...

Code:

function GetTagExp(pTagName){
    var temp = "<" + pTagName + ">((?:[^<]*|<(?:";
    for(var i = 0; i < pTagName.length; i++){
        temp += pTagName.substr(0, i);
        temp += "[^" + pTagName.charAt(i) + "]|";
    }
    temp += pTagName + "[^\\s>]+)[^>]*>)*)";
    temp += "</" + pTagName + ">";
    return temp;
}

HTH,

Chris


alex_read August 18th, 2004 03:44 AM

That is absolutely fantastic!!!!

Thanks a million Chris, that's great!



All times are GMT -4. The time now is 12:14 AM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.