PDA

View Full Version : regExp


duniyadnd
05-08-2003, 01:15 AM
I got the following code:


var pattern = "/java/i";
if (pattern.test("JavaScript"))
document.write("yup");
else
document.write("nope");



But I keep getting this error with this code:

Object doesn't support this property or method.

Any suggestions of what's going wrong?

Duniyadnd

A1ien51
05-08-2003, 01:46 AM
get rid of your quotes in the var pattern line

cheesebagpipe
05-08-2003, 04:50 AM
Just an elaboration: you can create a regular expression using a string - regexes are not strings - by calling the constructor:

var pattern = new RegExp("java" , "i");

The forward slashes (pair) indicate a regular expression literal - no constructor needed:

var pattern = /java/i;

Without either usage - no regex, so no test() method ('Object doesn't support this property or method').

http://www.webreference.com/js/column5/

duniyadnd
05-08-2003, 08:59 AM
Very Strange, cause the Javascript reference book I'm using has quotations.

Thanks for the help guys, works fine now.

Duniyadnd