Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #21584 > unrolled thread

regular expression

Started by"Tony Johansson" <johansson.andersson@telia.com>
First post2013-10-10 20:17 +0200
Last post2013-10-16 10:19 -0700
Articles 11 — 6 participants

Back to article view | Back to comp.lang.javascript


Contents

  regular expression "Tony Johansson" <johansson.andersson@telia.com> - 2013-10-10 20:17 +0200
    Re: regular expression "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2013-10-10 20:29 +0200
    Re: regular expression Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-10-10 21:33 +0200
      Re: regular expression Ben Bacarisse <ben.usenet@bsb.me.uk> - 2013-10-10 21:31 +0100
        Re: regular expression Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-10-10 22:57 +0200
          Re: regular expression Ben Bacarisse <ben.usenet@bsb.me.uk> - 2013-10-10 23:56 +0100
            Re: regular expression Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-10-11 01:06 +0200
              Re: regular expression Ben Bacarisse <ben.usenet@bsb.me.uk> - 2013-10-11 01:21 +0100
    Re: regular expression RobG <rgqld@iinet.net.au> - 2013-10-10 18:48 -0700
      Re: regular expression Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-10-11 11:42 +0200
    Re: regular expression dhtml <dhtmlkitchen@gmail.com> - 2013-10-16 10:19 -0700

#21584 — regular expression

From"Tony Johansson" <johansson.andersson@telia.com>
Date2013-10-10 20:17 +0200
Subjectregular expression
Message-ID<l36qvt$6rq$1@dont-email.me>
I want to filter this expression 
http://localhost:49507/slutProjekt/img/TwinBagel.jpg
by using regular expression so that I only want the string that starts after 
the last / and end before the dot.
In this example I want just TwinBagel.
I have this expression elmnt.src.replace(/^.*[\\\/]/, '');
but this will also return the string after the dot which I don't want.

//Tony 

[toc] | [next] | [standalone]


#21585

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2013-10-10 20:29 +0200
Message-ID<XnsA255D074A7B7Feejj99@194.109.133.133>
In reply to#21584
Tony Johansson wrote on 10 okt 2013 in comp.lang.javascript:

> I want to filter this expression 
> http://localhost:49507/slutProjekt/img/TwinBagel.jpg
> by using regular expression so that I only want the string that starts
> after the last / and end before the dot.
> In this example I want just TwinBagel.
> I have this expression elmnt.src.replace(/^.*[\\\/]/, '');
> but this will also return the string after the dot which I don't want.

var result = yourString.replace(/(^.*\/)|(\.[^]*$)/g, '');



-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

[toc] | [prev] | [next] | [standalone]


#21586

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2013-10-10 21:33 +0200
Message-ID<1692823.iOv17HgRNK@PointedEars.de>
In reply to#21584
Tony Johansson wrote:

> I want to filter this expression
> http://localhost:49507/slutProjekt/img/TwinBagel.jpg

That is _not_ an expression.  It is a URI.  It would be an expression if you 
enclosed it in string delimiters.

> by using regular expression so that I only want the string that starts
> after the last / and end before the dot.
> In this example I want just TwinBagel.
> I have this expression elmnt.src.replace(/^.*[\\\/]/, '');
> but this will also return the string after the dot which I don't want.

  var s = elmnt.src.replace(/^.*\/([^.]+).*/, '$1');

Better:

  var m = (elmnt.src.match(/([^.\/]+)(?:\..*?)?$/) || [])[1];

See also:

<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String>
<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp>.

AISB, use <http://tools.ietf.org/html/rfc3986#appendix-B> as template for 
parsing URIs.

-- 
PointedEars

Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

[toc] | [prev] | [next] | [standalone]


#21587

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2013-10-10 21:31 +0100
Message-ID<0.59968aeff2274c0aa6ab.20131010213157BST.8738o827gi.fsf@bsb.me.uk>
In reply to#21586
Thomas 'PointedEars' Lahn <PointedEars@web.de> writes:

> Tony Johansson wrote:
>> I want to filter this expression
>> http://localhost:49507/slutProjekt/img/TwinBagel.jpg
<snip>
>> by using regular expression so that I only want the string that starts
>> after the last / and end before the dot.
<snip>

>   var s = elmnt.src.replace(/^.*\/([^.]+).*/, '$1');
>
> Better:
>
>   var m = (elmnt.src.match(/([^.\/]+)(?:\..*?)?$/) || [])[1];

Not if there is an earlier '.' in the string.  I think your first RE
will do just as well here, though the trailing .* is no longer needed:

  var m = (elmnt.src.match(/^.*\/([^.]+)/) || [])[1];

<snip>
-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#21588

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2013-10-10 22:57 +0200
Message-ID<4522206.f2d4svmm8S@PointedEars.de>
In reply to#21587
Ben Bacarisse wrote:

> Thomas 'PointedEars' Lahn <PointedEars@web.de> writes:
>> Tony Johansson wrote:
>>> I want to filter this expression
>>> http://localhost:49507/slutProjekt/img/TwinBagel.jpg
> <snip>
>>> by using regular expression so that I only want the string that starts
>>> after the last / and end before the dot.
> <snip>
> 
>>   var s = elmnt.src.replace(/^.*\/([^.]+).*/, '$1');
>>
>> Better:
>>
>>   var m = (elmnt.src.match(/([^.\/]+)(?:\..*?)?$/) || [])[1];
> 
> Not if there is an earlier '.' in the string.

It was not intended as a general solution.

> I think your first RE will do just as well here, though the trailing .* is
> no longer needed:
> 
>   var m = (elmnt.src.match(/^.*\/([^.]+)/) || [])[1];

This is simpler and more general, but less efficient.

-- 
PointedEars

Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

[toc] | [prev] | [next] | [standalone]


#21589

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2013-10-10 23:56 +0100
Message-ID<0.db90040c6be3639c247a.20131010235624BST.87r4bszqef.fsf@bsb.me.uk>
In reply to#21588
Thomas 'PointedEars' Lahn <PointedEars@web.de> writes:

> Ben Bacarisse wrote:
>
>> Thomas 'PointedEars' Lahn <PointedEars@web.de> writes:
>>> Tony Johansson wrote:
>>>> I want to filter this expression
>>>> http://localhost:49507/slutProjekt/img/TwinBagel.jpg
>> <snip>
>>>> by using regular expression so that I only want the string that starts
>>>> after the last / and end before the dot.
>> <snip>
>> 
>>>   var s = elmnt.src.replace(/^.*\/([^.]+).*/, '$1');
>>>
>>> Better:
>>>
>>>   var m = (elmnt.src.match(/([^.\/]+)(?:\..*?)?$/) || [])[1];
>> 
>> Not if there is an earlier '.' in the string.
>
> It was not intended as a general solution.

Oh, OK.  That's odd, but it's yours to define "better" as a less general
solution.

>> I think your first RE will do just as well here, though the trailing .* is
>> no longer needed:
>> 
>>   var m = (elmnt.src.match(/^.*\/([^.]+)/) || [])[1];
>
> This is simpler and more general, but less efficient.

That's interesting.  What makes it less efficient?

-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#21591

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2013-10-11 01:06 +0200
Message-ID<2009598.r5QypLNUnr@PointedEars.de>
In reply to#21589
Ben Bacarisse wrote:

> Thomas 'PointedEars' Lahn <PointedEars@web.de> writes:
>> Ben Bacarisse wrote:
>>> Thomas 'PointedEars' Lahn <PointedEars@web.de> writes:
>>>> Tony Johansson wrote:
>>>>> I want to filter this expression
>>>>> http://localhost:49507/slutProjekt/img/TwinBagel.jpg
>>> <snip>
>>>>> by using regular expression so that I only want the string that starts
>>>>> after the last / and end before the dot.
>>> <snip>
>>> 
>>>>   var s = elmnt.src.replace(/^.*\/([^.]+).*/, '$1');
>>>>
>>>> Better:
>>>>
>>>>   var m = (elmnt.src.match(/([^.\/]+)(?:\..*?)?$/) || [])[1];
>>> 
>>> Not if there is an earlier '.' in the string.
>>
>> It was not intended as a general solution.
> 
> Oh, OK.  That's odd, but it's yours to define "better" as a less general
> solution.

Using String.prototype.match() is better than String.prototype.replace() 
here because it is the *direct* solution to the problem of extracting a 
substring by using pattern matching.

>>> I think your first RE will do just as well here, though the trailing .*
>>> is no longer needed:
>>> 
>>>   var m = (elmnt.src.match(/^.*\/([^.]+)/) || [])[1];
>>
>> This is simpler and more general, but less efficient.
> 
> That's interesting.  What makes it less efficient?

For a start, it has to match almost the entire input when only the last 
fragment of it is needed.
 
-- 
PointedEars

Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

[toc] | [prev] | [next] | [standalone]


#21592

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2013-10-11 01:21 +0100
Message-ID<0.8bf48427be7708670a6e.20131011012144BST.87k3hkzmg7.fsf@bsb.me.uk>
In reply to#21591
Thomas 'PointedEars' Lahn <PointedEars@web.de> writes:

> Ben Bacarisse wrote:
>
>> Thomas 'PointedEars' Lahn <PointedEars@web.de> writes:
>>> Ben Bacarisse wrote:
>>>> Thomas 'PointedEars' Lahn <PointedEars@web.de> writes:
<snip>
>>>>>   var s = elmnt.src.replace(/^.*\/([^.]+).*/, '$1');
>>>>>
>>>>> Better:
>>>>>
>>>>>   var m = (elmnt.src.match(/([^.\/]+)(?:\..*?)?$/) || [])[1];
>>>> 
>>>> Not if there is an earlier '.' in the string.
>>>
>>> It was not intended as a general solution.
>> 
>> Oh, OK.  That's odd, but it's yours to define "better" as a less general
>> solution.
>
> Using String.prototype.match() is better than String.prototype.replace() 
> here because it is the *direct* solution to the problem of extracting a 
> substring by using pattern matching.

Yes, of course.  That was clear.  I don't want to get into a discussion
about "better" -- it's for you to define since you used the term.

<snip> 
>>>>   var m = (elmnt.src.match(/^.*\/([^.]+)/) || [])[1];
>>>
>>> This is simpler and more general, but less efficient.
>> 
>> That's interesting.  What makes it less efficient?
>
> For a start, it has to match almost the entire input when only the last 
> fragment of it is needed.

Is matching, rather than looking for a match, so very significant?  Your
other RE may have to do more work looking for a match.  I say may
because that are a lot of unknowns in how and implementation optimises
REs.  It's not obvious to me just from looking at it that the second is
less efficient.  I don't suppose you are interested in some
measurements?

-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#21593

FromRobG <rgqld@iinet.net.au>
Date2013-10-10 18:48 -0700
Message-ID<43964690-058c-4ee0-9c0b-c2ba4dc7d58d@googlegroups.com>
In reply to#21584
On Friday, October 11, 2013 4:17:28 AM UTC+10, Tony Johansson wrote:
> I want to filter this expression 
> http://localhost:49507/slutProjekt/img/TwinBagel.jpg
> by using regular expression so that I only want the string that starts after 
> the last / and end before the dot.
> In this example I want just TwinBagel.
> I have this expression elmnt.src.replace(/^.*[\\\/]/, '');
> but this will also return the string after the dot which I don't want.

I not a fan of strategies that result in overly complex regular expressions or are unnecessarily restrictive on the input.

Consider using two steps:

  // Get the characters after the last /
  var m = s.match(/[^\/]+$/g);
  
  // Remove characters after the last '.', guarding against
  // errors if no match was found
  var result = m && m[0].replace(/\.[^\.]*$/,'');

The above won't throw an error in the case that match fails to find a match (and returns null), and will also allow for file names like "Receipts.2013.10.12.csv", but it will unconditionally remove everything after the last period character (.).

If no match is found, it will return null. 

-- 
Rob

[toc] | [prev] | [next] | [standalone]


#21597

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2013-10-11 11:42 +0200
Message-ID<1414309.6jTnyD6Tof@PointedEars.de>
In reply to#21593
RobG wrote:

> On Friday, October 11, 2013 4:17:28 AM UTC+10, Tony Johansson wrote:
>> I want to filter this expression
>> http://localhost:49507/slutProjekt/img/TwinBagel.jpg
>> by using regular expression so that I only want the string that starts
>> after the last / and end before the dot.
>> In this example I want just TwinBagel.
>> I have this expression elmnt.src.replace(/^.*[\\\/]/, '');
>> but this will also return the string after the dot which I don't want.
                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> I not a fan of strategies that result in overly complex regular
> expressions or are unnecessarily restrictive on the input.
> 
> Consider using two steps:
> 
>   // Get the characters after the last /
>   var m = s.match(/[^\/]+$/g);

The global modifier is pointless here.

>   // Remove characters after the last '.', guarding against
>   // errors if no match was found
>   var result = m && m[0].replace(/\.[^\.]*$/,'');

“.” does not need to be escaped in a character class in order to mean a 
literal dot.
 
> The above won't throw an error in the case that match fails to find a
> match (and returns null), and will also allow for file names like
> "Receipts.2013.10.12.csv", but it will unconditionally remove everything
> after the last period character (.).

Which is _not_ what was asked for.

-- 
PointedEars

Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

[toc] | [prev] | [next] | [standalone]


#21626

Fromdhtml <dhtmlkitchen@gmail.com>
Date2013-10-16 10:19 -0700
Message-ID<3d96173f-0995-4784-9652-195e90c8459d@googlegroups.com>
In reply to#21584
On Thursday, October 10, 2013 11:17:28 AM UTC-7, Tony Johansson wrote:
> I want to filter this expression 
> 
> http://localhost:49507/slutProjekt/img/TwinBagel.jpg
> 
> by using regular expression so that I only want the string that starts after 
> 
> the last / and end before the dot.
> 

Why must a RegExp be used?

> In this example I want just TwinBagel.
> 
> I have this expression elmnt.src.replace(/^.*[\\\/]/, '');
> 
> but this will also return the string after the dot which I don't want.
> 
a.substring(a.lastIndexOf("/")+1, a.lastIndexOf("."));
-- 
Garrett
@dhtmlkitchen
ChordCycles.tumblr.com

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web