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


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

Regular expression

Started by"Tony" <tony.johansson@inport.com>
First post2013-04-25 18:04 +0200
Last post2013-04-26 21:17 +0200
Articles 7 — 4 participants

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


Contents

  Regular expression "Tony" <tony.johansson@inport.com> - 2013-04-25 18:04 +0200
    Re: Regular expression Scott Sauyet <scott.sauyet@gmail.com> - 2013-04-25 10:07 -0700
      Re: Regular expression "Tony" <tony.johansson@inport.com> - 2013-04-26 11:27 +0200
    Re: Regular expression Denis McMahon <denismfmcmahon@gmail.com> - 2013-04-25 18:05 +0000
      Re: Regular expression Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-04-25 21:02 +0200
      Re: Regular expression Scott Sauyet <scott.sauyet@gmail.com> - 2013-04-26 11:15 -0700
        Re: Regular expression Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-04-26 21:17 +0200

#19468 — Regular expression

From"Tony" <tony.johansson@inport.com>
Date2013-04-25 18:04 +0200
SubjectRegular expression
Message-ID<klbk00$8ks$1@dont-email.me>
myDate has the following format ex. 2013/04/22 03:34
I want to use this pattern
var m = myDate.match(/^\d{4}-\d\d-\d\d (\d\d):(\d\d)$/),
        hour = +m[1],
        min  = +m[2];

But before I can use this pattern
I need to change the - to / because the pattern works for format like
format ex. 2013-04-22 03:34

My first attempt is to replace the - with / but when  I do so
I get green underline and invalid character
var m = myDate.match(/^\d{4}/\d\d/\d\d (\d\d):(\d\d)$/),
             hour = +m[1],
             min  = +m[2];

My second attempt is to use escape character.
So I add the escape character \ immediately before character / but when I do 
so
I get runtime error
var m = myDate.match(/^\d{4}\/\d\d\/\d\d (\d\d):(\d\d)$/),
             hour = +m[1],
             min  = +m[2];

So how should I write to replace - with / in the pattern

//Tony 

[toc] | [next] | [standalone]


#19471

FromScott Sauyet <scott.sauyet@gmail.com>
Date2013-04-25 10:07 -0700
Message-ID<4fcb1664-8b09-4961-863d-e1232eccef64@n4g2000yqj.googlegroups.com>
In reply to#19468
On Apr 25, 12:04 pm, "Tony" <tony.johans...@inport.com> wrote:

> myDate has the following format ex. 2013/04/22 03:34 [ ... ]
> I get runtime error [ ... ]
> var m = myDate.match(/^\d{4}\/\d\d\/\d\d (\d\d):(\d\d)$/),
>              hour = +m[1],
>              min  = +m[2];
> [ ... ]

Can you describe the runtime error you see, and the environment?  This
code works fine for me:

    var myDate = "2013/04/22 03:34";
    var m = myDate.match(/^\d{4}\/\d\d\/\d\d (\d\d):(\d\d)$/),
        hour = +m[1],
        min  = +m[2];

and sets `hour` to 3 and `min` to 34.  I only tested in  Chrome's
console, but at a casual glance I can't see any reason it shouldn't
work in other environments.

  -- Scott

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


#19482

From"Tony" <tony.johansson@inport.com>
Date2013-04-26 11:27 +0200
Message-ID<kldh3k$u49$1@dont-email.me>
In reply to#19471

"Scott Sauyet"  wrote in message 
news:4fcb1664-8b09-4961-863d-e1232eccef64@n4g2000yqj.googlegroups.com...

On Apr 25, 12:04 pm, "Tony" <tony.johans...@inport.com> wrote:

> myDate has the following format ex. 2013/04/22 03:34 [ ... ]
> I get runtime error [ ... ]
> var m = myDate.match(/^\d{4}\/\d\d\/\d\d (\d\d):(\d\d)$/),
>              hour = +m[1],
>              min  = +m[2];
> [ ... ]

Can you describe the runtime error you see, and the environment?  This
code works fine for me:

    var myDate = "2013/04/22 03:34";
    var m = myDate.match(/^\d{4}\/\d\d\/\d\d (\d\d):(\d\d)$/),
        hour = +m[1],
        min  = +m[2];

and sets `hour` to 3 and `min` to 34.  I only tested in  Chrome's
console, but at a casual glance I can't see any reason it shouldn't
work in other environments.

  -- Scott

It was typo error from myself

//Tony 

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


#19472

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2013-04-25 18:05 +0000
Message-ID<klbr98$2pp$1@dont-email.me>
In reply to#19468
On Thu, 25 Apr 2013 18:04:12 +0200, Tony wrote:

> So how should I write to replace - with / in the pattern

s = s.replace( /-/g, "/" );

JavaScript[0] regex is *not* pcre, and brackets or braces "(" and ")" to 
capture subsets of the re is not supported.

See http://www.sined.co.uk/tmp/jsregex.htm for a possible solution.

[0] Before you even start on the fact I said "JavaScript", just fuck off 
now pointy thing!

-- 
Denis McMahon, denismfmcmahon@gmail.com

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


#19474

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2013-04-25 21:02 +0200
Message-ID<4180203.T6N6VvZNV1@PointedEars.de>
In reply to#19472
Denis McMahon wrote:

> On Thu, 25 Apr 2013 18:04:12 +0200, Tony wrote:
>> So how should I write to replace - with / in the pattern
> 
> s = s.replace( /-/g, "/" );
> 
> JavaScript[0] regex is *not* pcre,

Correct.

> and brackets or braces "(" and ")" to capture subsets of the re is not
> supported.

Wrong again.
 
> See http://www.sined.co.uk/tmp/jsregex.htm for a possible solution.

No, don't.

-- 
PointedEars

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

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


#19494

FromScott Sauyet <scott.sauyet@gmail.com>
Date2013-04-26 11:15 -0700
Message-ID<9b84bbcd-e48d-4996-81ab-137e1e9520f3@q9g2000yqd.googlegroups.com>
In reply to#19472
Denis McMahon wrote:
>
> JavaScript [...] regex is *not* pcre,

This is true.

> and brackets or braces "(" and ")" to capture subsets of the re
> is not supported.

I don't know where you got this idea. See, for instance,

    <http://ecma-international.org/ecma-262/5.1/#sec-15.10>, or
    <https://developer.mozilla.org/en-US/docs/JavaScript/Guide/
Regular_Expressions#Using_Parenthesized_Substring_Matches>

  -- Scott

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


#19495

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2013-04-26 21:17 +0200
Message-ID<1591684.Q6jD1YjAGM@PointedEars.de>
In reply to#19494
Scott Sauyet wrote:

> Denis McMahon wrote:
>> and brackets or braces "(" and ")" to capture subsets of the re
>> is not supported.
> 
> I don't know where you got this idea. See, for instance,
> 
>     <http://ecma-international.org/ecma-262/5.1/#sec-15.10>, or

Perhaps referring to the relevant production would be more convincing:

,<http://ecma-international.org/ecma-262/5.1/#sec-15.10.2.8>
|
| […]
| 
| The production Atom :: ( Disjunction ) evaluates as follows: […]

>     <https://developer.mozilla.org/en-US/docs/JavaScript/Guide/
> Regular_Expressions#Using_Parenthesized_Substring_Matches>

-- 
PointedEars

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

[toc] | [prev] | [standalone]


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


csiph-web