Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > regex - why won't this work?

Reply
Thread Tools

regex - why won't this work?

 
 
Trev
Guest
Posts: n/a
 
      10-26-2006
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

 
Reply With Quote
 
 
 
 
p.lepin@ctncorp.com
Guest
Posts: n/a
 
      10-26-2006

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

 
Reply With Quote
 
 
 
 
Trev
Guest
Posts: n/a
 
      10-26-2006

(E-Mail Removed) 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

 
Reply With Quote
 
Lee
Guest
Posts: n/a
 
      10-26-2006
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");


--

 
Reply With Quote
 
shimmyshack
Guest
Posts: n/a
 
      10-26-2006
var stringtosearch = "ARTHUR and GEORGE's guest house";
var reg = /arthur/i;
if ( reg.test( stringtosearch ) == true )
{
alert( 'yippee' );
}
else
{
alert( 'nope' );
}

 
Reply With Quote
 
Tom Cole
Guest
Posts: n/a
 
      10-26-2006

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.

 
Reply With Quote
 
Trev
Guest
Posts: n/a
 
      10-26-2006
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.....

 
Reply With Quote
 
mick white
Guest
Posts: n/a
 
      10-26-2006
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);
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How make regex that means "contains regex#1 but NOT regex#2" ?? seberino@spawar.navy.mil Python 3 07-01-2008 03:06 PM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Is ASP Validator Regex Engine Same As VS2003 Find Regex Engine? =?Utf-8?B?SmViQnVzaGVsbA==?= ASP .Net 2 10-22-2005 02:43 PM
perl regex to java regex Rick Venter Java 5 11-06-2003 10:55 AM



Advertisments