regex - why won't this work?

Discussion in 'Javascript' started by Trev, Oct 26, 2006.

  1. Trev

    Trev Guest

    Hi everyone,
    I've got a very simple piece of code, and it doesn't work. I'm stumped!

    var pattern1="arthur/i";

    var stringtosearch = "ARTHUR and GEORGE's guest house";

    pattern1.test(stringtosearch);

    This should return true, but it returns false! Can anyone see what is
    wrong?

    TIA

    Trev
     
    Trev, Oct 26, 2006
    #1
    1. Advertising

  2. Trev

    Guest

    Trev wrote:
    > var pattern1="arthur/i";
    >
    > var stringtosearch = "ARTHUR and GEORGE's guest house";
    >
    > pattern1.test(stringtosearch);
    >
    > This should return true, but it returns false!


    Actually, it should say something along the lines of
    'pattern1.test is not a function'.

    > Can anyone see what is wrong?


    Sure, replace

    var pattern1="arthur/i";

    with

    var pattern1 = /arthur/i ;

    --
    Pavel Lepin
     
    , Oct 26, 2006
    #2
    1. Advertising

  3. Trev

    Trev Guest

    wrote:
    > Trev wrote:
    > > var pattern1="arthur/i";
    > >
    > > var stringtosearch = "ARTHUR and GEORGE's guest house";
    > >
    > > pattern1.test(stringtosearch);
    > >
    > > This should return true, but it returns false!

    >
    > Actually, it should say something along the lines of
    > 'pattern1.test is not a function'.
    >
    > > Can anyone see what is wrong?

    >
    > Sure, replace
    >
    > var pattern1="arthur/i";
    >
    > with
    >
    > var pattern1 = /arthur/i ;
    >
    > --
    > Pavel Lepin



    Thanks Pavel; I've tried that but it still gives false :(
     
    Trev, Oct 26, 2006
    #3
  4. Trev

    Lee Guest

    Trev said:
    >
    >Hi everyone,
    >I've got a very simple piece of code, and it doesn't work. I'm stumped!
    >
    >var pattern1="arthur/i";
    >
    >var stringtosearch = "ARTHUR and GEORGE's guest house";
    >
    >pattern1.test(stringtosearch);
    >
    >This should return true, but it returns false! Can anyone see what is
    >wrong?


    Actually, what that should do is tell you that pattern1.test is not
    a function, because "arthur/i" is not a RegExp.
    It's just a string that happens to end in "/i".

    var pattern1=/arthur/i;
    or
    var pattern1 = new RegExp("arthur","i");


    --
     
    Lee, Oct 26, 2006
    #4
  5. Trev

    shimmyshack Guest

    var stringtosearch = "ARTHUR and GEORGE's guest house";
    var reg = /arthur/i;
    if ( reg.test( stringtosearch ) == true )
    {
    alert( 'yippee' );
    }
    else
    {
    alert( 'nope' );
    }
     
    shimmyshack, Oct 26, 2006
    #5
  6. Trev

    Tom Cole Guest

    shimmyshack wrote:
    > var stringtosearch = "ARTHUR and GEORGE's guest house";
    > var reg = /arthur/i;
    > if ( reg.test( stringtosearch ) == true )
    > {
    > alert( 'yippee' );
    > }
    > else
    > {
    > alert( 'nope' );
    > }


    Trev,

    Notice that the regex does not have quotes around it. If you quote it,
    it is not a regex, it becomes a string. String does not have a test
    method AFAIK.

    HTH.
     
    Tom Cole, Oct 26, 2006
    #6
  7. Trev

    Trev Guest

    Thanks - I figured out what had gone wrong (missed out a trailing ";").
    Grrrrr!

    Now for my next trick:
    var myURL = document.referrer;
    var mySearch = /george/i;

    and then do a check on mySearch.test(myURL)....should work OK.....
     
    Trev, Oct 26, 2006
    #7
  8. Trev

    mick white Guest

    Trev wrote:

    > Thanks - I figured out what had gone wrong (missed out a trailing ";").
    > Grrrrr!
    >
    > Now for my next trick:
    > var myURL = document.referrer;
    > var mySearch = /george/i;
    >
    > and then do a check on mySearch.test(myURL)....should work OK.....
    >

    var d;
    var containsGeorge=
    (d=document.referrer) && /george/i.test(d);
     
    mick white, Oct 26, 2006
    #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. Marina

    Re: WHY, WHY WON'T IT WORK???

    Marina, Jun 29, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    369
    Marina
    Jun 29, 2004
  2. Chad
    Replies:
    4
    Views:
    8,420
  3. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    974
    Mark Rae
    Dec 21, 2006
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,213
    Smokey Grindel
    Dec 2, 2006
  5. Replies:
    15
    Views:
    174
    Ilya Zakharevich
    Apr 17, 2008
Loading...

Share This Page