473,320 Members | 1,612 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

regex - why won't this work?

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

Oct 26 '06 #1
7 1426

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 '06 #2

p.le...@ctncorp.com 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 :(

Oct 26 '06 #3
var stringtosearch = "ARTHUR and GEORGE's guest house";
var reg = /arthur/i;
if ( reg.test( stringtosearch ) == true )
{
alert( 'yippee' );
}
else
{
alert( 'nope' );
}

Oct 26 '06 #4

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.

Oct 26 '06 #5
Lee
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");
--

Oct 26 '06 #6
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.....

Oct 26 '06 #7
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);
Oct 26 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Shmulik | last post by:
How can I (can I?) use a regular expression as the filter to a FileSystemWatcher? I want to watch for something like this: Regex regex = new Regex(@"^Current(One|Two|Three)File\.txt$",...
4
by: aevans1108 | last post by:
expanding this message to microsoft.public.dotnet.xml Greetings Please direct me to the right group if this is an inappropriate place to post this question. Thanks. I want to format a...
4
by: H | last post by:
This is kind of an followup on oneof my previous questions, and it has with RegEx to do. I have a string containing of several words. What would a good regex expression looklike to get one match...
16
by: Andrew Baker | last post by:
I am trying to write a function which provides my users with a file filter. The filter used to work just using the VB "Like" comparision, but I can't find the equivilant in C#. I looked at...
7
by: bill tie | last post by:
I'd appreciate it if you could advise. 1. How do I replace "\" (backslash) with anything? 2. Suppose I want to replace (a) every occurrence of characters "a", "b", "c", "d" with "x", (b)...
7
by: Razzie | last post by:
Hey all, Decided to give a shot at Regular expressions - need a bit of help :) I can't seem to find the right regex for matching words like "*test*" or *somevalue*" - in short, all words...
7
by: melanieab | last post by:
Hi, I'm trying to use DataView to find the row number in the datatable that contains "Rich" in it so that I can highlight it. It works fine when I enter the entire string (i.e. Richard), but I...
3
by: jab3 | last post by:
Hello. I"m new to this group, and to JavaScript in general, so please forgive me if I breach local etiquette. I'm trying to implement some client-side 'dynamic' validation on a form. I'm having...
11
by: Steve | last post by:
Hi All, I'm having a tough time converting the following regex.compile patterns into the new re.compile format. There is also a differences in the regsub.sub() vs. re.sub() Could anyone lend...
6
by: Extremest | last post by:
I have a huge regex setup going on. If I don't do each one by itself instead of all in one it won't work for. Also would like to know if there is a faster way tried to use string.replace with all...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.