Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!newsfeed.straub-nv.de!uucp.gnuu.de!newsfeed.arcor.de!newsspool4.arcor-online.net!news.arcor.de.POSTED!not-for-mail Content-Type: text/plain; charset="UTF-8" Message-ID: <2997930.SPkdTlGXAF@PointedEars.de> From: Thomas 'PointedEars' Lahn Reply-To: Thomas 'PointedEars' Lahn Organization: PointedEars Software (PES) Date: Wed, 26 Oct 2011 23:40:37 +0200 User-Agent: KNode/4.4.11 Content-Transfer-Encoding: 8Bit Subject: Re: Regular expression question Newsgroups: comp.lang.javascript References: Followup-To: comp.lang.javascript MIME-Version: 1.0 Lines: 61 NNTP-Posting-Date: 26 Oct 2011 23:40:37 CEST NNTP-Posting-Host: 387bf7c1.newsspool1.arcor-online.net X-Trace: DXC==n_a[?]>Q_DlU`@c^jLCbJic==]BZ:afN4Fo<]lROoRA<`=YMgDjhgB5EF936Ae=FDDZm8W4\YJNL;?f@h5gMfbLe cerr writes: >> First thing, I'm a regular expression newbie.... somewhat anyways... >> I would like to recognize the difference between this url: >> http://quaaoutlodge.com/site/the-lodge/our-history.html >> and that url: >> http://quaaoutlodge.com/site/the-lodge.html >> and at the same time extract the document name (our-history or the- >> lodge) and the directory name if present (the-lodge). >> I got stuck at how rto rcognize the second directory instead of the >> first (the-lodge/ instead of site/) with "\b\/[a-z]+\/" how do i get >> the second one only? > > When you think a RegExp might solve your problem - stop for a moment > and think whether there is also a simpler solution :) I cannot think of anything that is simpler than var matches = url.match(/(.*)\/([^\/]+)$/); and then have a look at matches[1] ("directory") and matches[2] ("document name"). But that's me. > In this case, I'd just do: > > function name(url) { That is a poor function identifier. > var name_end = url.lastIndexOf("."); > var name_start = url.lastIndexOf("/", name_end) + 1; Paths may contain dots. Resource names do not need to. > return url.substr(name_start, name_end); You meant return url.substring(name_start, name_end); String.prototyp.substr(), OTOH, is proprietary – which is why it should not be used – and has ifferent semantics: | B.2.3 String.prototype.substr (start, length) > } > > If your URLs aren't always that simple, you'd need to adapt a RegExp too. The general solution to this problem is so simple that you really could have posted it (BTDT). OTOH, that is also why the OP could have found it by STFW. PointedEars -- Prototype.js was written by people who don't know javascript for people who don't know javascript. People who don't know javascript are not the best source of advice on designing systems that use javascript. -- Richard Cornford, cljs,