473,228 Members | 1,617 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,228 software developers and data experts.

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 1454
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Patryk Konieczka | last post by:
Hello Here's the thing I have a database edited by some company workers editing descriptions of books in the sotre , unfortunately these workers do not have the habit of inserting a space...
7
by: serpent17 | last post by:
Hello all, I have this line of numbers: 04242005 18:20:42-0.000002, 271.1748608, , , repeated several times in a text file and I would like each element to be part of a vector. how do...
1
by: joh12005 | last post by:
Hello, here is a trouble that i had, i would like to resolve it with python, even if i still have no clue on how to do it. i had many small "text" files, so to speed up processes on them, i...
3
by: Jane Doe | last post by:
Hello, I need to browse a list of hyperlinks, each followed by an author, and remove the links only for certain authors. 1. I searched the archives on Google, but didn't find how to tell the...
3
by: jasonkester | last post by:
Just a heads up for anybody that comes across this in the future. Noticed a strange behavior in RegExp.test() today. Check out the following code. It will alternately display "chokes" and null,...
7
by: Csaba Gabor | last post by:
I need to come up with a function function regExpPos (text, re, parenNum) { ... } that will return the position within text of RegExp.$parenNum if there is a match, and -1 otherwise. For...
4
by: conan | last post by:
This regexp '<widget class=".*" id=".*">' works well with 'grep' for matching lines of the kind <widget class="GtkWindow" id="window1"> on a XML .glade file However that's not true for the...
9
by: =?ISO-8859-1?Q?BJ=F6rn_Lindqvist?= | last post by:
With regexps you can search for strings matching it. For example, given the regexp: "foobar\d\d\d". "foobar123" would match. I want to do the reverse, from a regexp generate all strings that could...
4
by: Matt | last post by:
Hello all, I have just discovered (the long way) that using a RegExp object with the 'global' flag set produces inconsistent results when its test() method is executed. I realize that 'global'...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
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: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.