Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #7707
| Message-ID | <2997930.SPkdTlGXAF@PointedEars.de> (permalink) |
|---|---|
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
| Organization | PointedEars Software (PES) |
| Date | 2011-10-26 23:40 +0200 |
| Subject | Re: Regular expression question |
| Newsgroups | comp.lang.javascript |
| References | <a9fa509f-5f3c-4926-abc6-c77a21427d8f@j36g2000prh.googlegroups.com> <mxcnirot.fsf@gmail.com> |
| Followup-To | comp.lang.javascript |
Followups directed to: comp.lang.javascript
Lasse Reichstein Nielsen wrote:
> cerr <ron.eggler@gmail.com> 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, <f806at$ail$1$8300dec7@news.demon.co.uk>
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar
Regular expression question cerr <ron.eggler@gmail.com> - 2011-10-25 21:43 -0700
Re: Regular expression question Mike Duffy <never@you.mind.com> - 2011-10-26 12:11 +0000
Re: Regular expression question Denis McMahon <denismfmcmahon@gmail.com> - 2011-10-26 16:54 +0000
Re: Regular expression question Lasse Reichstein Nielsen <lrn.unread@gmail.com> - 2011-10-26 19:33 +0200
Re: Regular expression question Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-10-26 23:40 +0200
Re: Regular expression question Antony Scriven <adscriven@gmail.com> - 2011-10-27 17:30 -0700
Re: Regular expression question Antony Scriven <adscriven@gmail.com> - 2011-10-27 17:39 -0700
Re: Regular expression question Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-10-28 03:50 +0200
Re: Regular expression question Antony Scriven <adscriven@gmail.com> - 2011-10-28 08:46 -0700
Re: Regular expression question Lasse Reichstein Nielsen <lrn.unread@gmail.com> - 2011-10-28 18:50 +0200
Re: Regular expression question Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-10-28 19:50 +0200
Re: Regular expression question Antony Scriven <adscriven@gmail.com> - 2011-10-28 11:41 -0700
Re: Regular expression question Antony Scriven <adscriven@gmail.com> - 2011-10-28 12:54 -0700
Re: Regular expression question Dr J R Stockton <reply1143@merlyn.demon.co.uk> - 2011-10-27 19:41 +0100
Re: Regular expression question Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-10-28 04:12 +0200
csiph-web