Regular Expressions

Discussion in 'Javascript' started by Kalvin, Jan 31, 2005.

  1. Kalvin

    Kalvin Guest

    Please help me understand regular expressions. I have read different
    articles about them, they all seem to explain it differently.

    1.
    On one site it says to do it like this:
    return ( /^[0-9]+$/.test(num) )

    2.
    On another it says:
    var regExp = new RegExp(/^[a-zA-Z0-9]+$/)
    return ( regExp.test(str) )

    3.
    I see others that put the expression into a string and then test it.

    I have tried both 1 and 2, and on some clients 2 will work, but on
    others it always returns false no matter what. What's the difference
    between them? How do I know what is going to work on all clients?

    Is there a way to find out exactly what version of Javascript is being
    used on the client. Not from code, but just by looking somewhere on
    the machine. A lot of what I am trying to write this for is websites
    to be used internally.

    I appreciate your time.
    Kalvin
     
    Kalvin, Jan 31, 2005
    #1
    1. Advertising

  2. Kalvin

    SimonFx Guest

    Damn! I just started to rely on Regular Expressions ... what clients are
    causing the problem? I don't actually use parentheses like you do, and I
    think you are supposed to use quotes when doing the new RegExp thing.

    1.
    return /^[0-9]+$/.test(num)
    2.
    var regExp = new RegExp ("^[a-zA-Z0-9]+$")
    return regExp.test(str)

    Kalvin wrote:
    > 1.
    > return ( /^[0-9]+$/.test(num) )
    > 2.
    > var regExp = new RegExp(/^[a-zA-Z0-9]+$/)
    > return ( regExp.test(str) )


    > I have tried both 1 and 2, and on some clients 2 will work, but on
    > others it always returns false no matter what. What's the difference
    > between them? How do I know what is going to work on all clients?
     
    SimonFx, Feb 1, 2005
    #2
    1. Advertising

  3. Kalvin

    Matthew Lock Guest

    Apart from really old browsers the only difference I am aware of is
    that IE 5.0 doesn't support non-capturing parenthesis eg: (?:banana)
    There may be more differences but I haven't come across any myself.
     
    Matthew Lock, Feb 1, 2005
    #3
  4. Kalvin

    Matthew Lock Guest

    Apart from really old browsers the only difference I am aware of is
    that IE 5.0 doesn't support non-capturing parenthesis eg: (?:banana)
    There may be more differences but I haven't come across any myself.
     
    Matthew Lock, Feb 1, 2005
    #4
  5. Kalvin

    Kalvin Guest

    We use IE 6 and Netscape. In our company we use an image to rebuild
    machines, and so far have only come across one that example 2 just will
    not work. I used example 1 and it worked fine, so far. I really like
    regular expressions, and hope I don't have other problems later.

    Thank you everyone for your replies.

    BTW, is there a way to discover what version of javascript is on a
    machine?

    Kalvin
     
    Kalvin, Feb 1, 2005
    #5
  6. Kalvin

    sunami Guest

    Both ways of using the regulare expression are the same ... the first
    method is more consise and the RegExp object is created on-the-fly.
    The 2nd method is using a pre-defined RegExp object.

    If you're getting different results from different clients, it's
    probably because of the differences in the JavaScript engines. You can
    find out which version buy the client's version (like Firefox is
    implementing JavaScript 2 ... just an example .. i'm not sure which
    version it's implementing).

    a good resource on JavaScript:

    http://www.devguru.com/Technologies/ecmascript/quickref/javascript_index.html
     
    sunami, Feb 1, 2005
    #6
  7. Kalvin

    Mick White Guest

    Kalvin wrote:
    [snip]


    > var regExp = new RegExp(/^[a-zA-Z0-9]+$/)
    > return ( regExp.test(str) )


    return new RegExp("^[a-z0-9]+$","i").test(str);

    Is the preferred construct, I believe.
    Mick

    [snip]
     
    Mick White, Feb 1, 2005
    #7
  8. JRS: In article <>
    , dated Tue, 1 Feb 2005 07:25:35, seen in news:comp.lang.javascript,
    Kalvin <> posted :
    >
    >BTW, is there a way to discover what version of javascript is on a
    >machine?


    <URL:http://www.merlyn.demon.co.uk/js-other.htm>
    <URL:http://www.merlyn.demon.co.uk/js-tests.htm>

    But such tests do not necessarily work on all versions.

    Read newsgroup FAQ 4.26 and its Notes.

    --
    © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
    <URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
     
    Dr John Stockton, Feb 1, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jay Douglas

    Custom Regular Expressions in ASP.net

    Jay Douglas, Nov 2, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    619
    mikeb
    Nov 3, 2003
  2. mark

    Regular expressions

    mark, Jun 30, 2003, in forum: Perl
    Replies:
    4
    Views:
    1,737
  3. Dustin D.
    Replies:
    1
    Views:
    11,384
  4. Jay Douglas
    Replies:
    0
    Views:
    644
    Jay Douglas
    Aug 15, 2003
  5. Noman Shapiro
    Replies:
    0
    Views:
    262
    Noman Shapiro
    Jul 17, 2013
Loading...

Share This Page