470,811 Members | 1,266 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,811 developers. It's quick & easy.

regexp not matching variable

I have the following code that matches just fine.

var re;
re = /23-Apr-2004/gi;
alert(re.test(document.all['thebody'].innerHTML));

However, when I attempt it with a variable containing the same characters it
thows the error
"Object doesn't support this property or method" on the test line below.

var months = new
Array('Jan','Feb','Mar','Apr','May','Jun','Jul','A ug','Sep','Oct','Nov','Dec
');
var now = new Date();
var today = now.getDate() + "-" + months[now.getMonth()] + "-" +
now.getYear();
today = "/"+today+"/gi";
var re;
re = today;
alert(re.test(document.all['thebody'].innerHTML));

Can anyone tell me why this isn't working and perhaps a way around it?
Jul 23 '05 #1
6 1403
Daniel McLaughlin wrote:
I have the following code that matches just fine.

var re;
re = /23-Apr-2004/gi;
alert(re.test(document.all['thebody'].innerHTML));

However, when I attempt it with a variable containing the same characters it
thows the error
"Object doesn't support this property or method" on the test line below.

var months = new
Array('Jan','Feb','Mar','Apr','May','Jun','Jul','A ug','Sep','Oct','Nov','Dec
');
var now = new Date();
var today = now.getDate() + "-" + months[now.getMonth()] + "-" +
now.getYear();
today = "/"+today+"/gi";
the reg ex would be:
eval("/"+today+"/gi");
Personally, I would not name the variable "today"
var re;
re = today;
alert(re.test(document.all['thebody'].innerHTML));
<body onload="alert(re.test(document.all['thebody'].innerHTML));">
Mick
Can anyone tell me why this isn't working and perhaps a way around it?

Jul 23 '05 #2
For dynamic or variable regular expressions you need to use the new
RegExp(pattern, modifiers) constructor not literals. The 'pattern' is
your 'today' string which leaves out those forward slashes, they're for
delimiting regular expression literals. This example should give you the
general idea.

var today = 'april' + '-' + '22' + '-' + '2004';
var rx = new RegExp(today, 'gi');


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 23 '05 #3
Mick White <mw******@rochester.rr.com> writes:
today = "/"+today+"/gi";

This just makes today a string, not a regexp ...
the reg ex would be:
eval("/"+today+"/gi");


I would recommend against using eval. Too many things can go wrong.
Use the RegExp constructor instead:
today = RegExp(today,"gi");

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #4
"Daniel McLaughlin" <ja********@denial.co.uk.nospam> wrote in message news:<10***************@news01.eclipse.net.uk>...
I have the following code that matches just fine.

var re;
re = /23-Apr-2004/gi;
alert(re.test(document.all['thebody'].innerHTML));

However, when I attempt it with a variable containing the same characters it
thows the error
"Object doesn't support this property or method" on the test line below.

var months = new
Array('Jan','Feb','Mar','Apr','May','Jun','Jul','A ug','Sep','Oct','Nov','Dec
');
var now = new Date();
var today = now.getDate() + "-" + months[now.getMonth()] + "-" +
now.getYear();
today = "/"+today+"/gi";
var re;
re = today;
alert(re.test(document.all['thebody'].innerHTML));

Can anyone tell me why this isn't working and perhaps a way around it?


-----------------------------

You cannot define your regular expression dynamically as such:
reg="/" + "abc" + "/"

This just doesn't work. At least not for me.
It is NOT a string.

You would define it as just
reg=/abc/

If you want to dynamically construct a regular expression, use
regex = new RegExp (pattern)

In this last example, you can define your pattern dynamically.
Jul 23 '05 #5
Lasse Reichstein Nielsen wrote:
Use the RegExp constructor instead: ^^^^^^^^^^^ today = RegExp(today,"gi");


Although RegExp(x, y) is specified in ECMAScript 3 as a factory,

today = new RegExp(today, "gi");

is saner.
PointedEars
Jul 23 '05 #6
bruce wrote:
You cannot define your regular expression dynamically as such:
reg="/" + "abc" + "/"

This just doesn't work. At least not for me.
For nobody. Atigs. (And that is good so.)
It is NOT a string.


That's why concatenation of strings does not create a RegExp object.
PointedEars
Jul 23 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Patryk Konieczka | last post: by
7 posts views Thread by serpent17 | last post: by
1 post views Thread by joh12005 | last post: by
3 posts views Thread by jasonkester | last post: by
7 posts views Thread by Csaba Gabor | last post: by
9 posts views Thread by =?ISO-8859-1?Q?BJ=F6rn_Lindqvist?= | last post: by
4 posts views Thread by Matt | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.