Question on this variable

Hi,

I have an example where the author does the following:

function mouseOut() {
var roll = [COLOR=“Red”]new RegExp /COLOR;
if (this.id.match(roll)) {

What’s the advantage of the “new RegExp”…
couldn’t you just as well say…
var roll=“rollover”?

as another alternative, couldn’t you just do the match on the literal, and skip the variable all together (i.e… if (this.id.match("rollover’)) {

Thanks,

Jeff

There’s not really any advantage except maybe, since it’s an example, showing off the functionality of RegExp, the Regular Expression Object constructor. (String.match() needs a regular expression as its argument, but it will convert strings into regular expressions if fed a string as an argument.) The sample code is the “cleanest” and most straight-forward way to achieve the goal, but it’s not the one that uses the fewest characters. If I were the one writing the code, I would say:

function mouseOut(){
	if(this.id.match(/rollover/)){
		// etc…

oh, great! The RegExp object constructor? The book doesn’t even talk about that at all…He just used it in this example, and I guess, figured I knew what he was talking about. I’ve used pattern matching in another language, so I’m familiar with the concept. But until you just mentioned the “RegExp object constructor”, I thought RegExp was just a made up name. What exactly is it doing that just saying "var roll = “rollout” isn’t doing?

Also, I guess this means, if there’s a “RegExp” object constructor, then there are probably several other “object constructors”? What tha heck is an object constructor? ( Maybe its a concept too advanced for this beginner’s book…)

btw, there was an earlier example in the book, where he didn’t use RegExp; and did the match similar to the way you coded in your response…
if (inputNum.match(/one|two|…|ten/))

It’s assigning a regular expression to the variable.

It’s an object that creates an instance of a new object.
Other types of objects that can be constructed are String, Number, Boolean, Array, Function, Date, Error

The constructor page gives some good details about them.

Thanks guys…I think I get it. In plain English, RegExp would convert my variable to a regular expression, prior to me attempting to do the match – if it were not already a regular expression, right? ( I’ll read the links you sent me a little more, and perhaps they’ll answer this next question – but can you give me an example of an expression that is not a regular expression?)

Thanks again,
Jeff

What do you mean by “expression”. For javascript, an expression is anything that evaluates to a single value, such as:
x = 2 + 2

In case that;'s not what you mean, I’ll diverge on to the two main areas of interest that normally follow on from what you’ve learned.

Regular expressions can be defined by using either the regular expression constructor, or by using a regular expression literal. The main reason to use the constructor instead of the literal is when the contents of the expression may not be known at the time of writing the code.

The other main areas of interest is in terms of other constructors. The Date constructor is one that’s most commonly used. For most of the rest, we most commonly use the literal manner instead.

What do you mean by “expression”.

I was asking for an example of an expression that was not a regular expression, since my understanding now of RegExp is that it converts my variable to a “Regular” expression. So, I was thinking – “regular” vs “what?” – what would be a “non-regular” expression?

And I was wondering what exactly the constructor did,but your explanation in your last response really cleared that up for me, and I greatly appreciate it.

Thanks much, Paul.

Jeff

Ahh, I see now. The question that might be most effectively answered is: why is it called a regular expression.

It’s not called a regular expression due to non-regular expressions. Instead, it all stems from regular sets from the 1950’s. Here’s some history on regular expressions that might help somewhat.

Regular expressions are used on a vast number of programming languages, which mostly all use the same syntax.

The best place that I’ve found to learn about regular expressions is at regular-expressions.info which has a good [url="http://www.regular-expressions.info/tutorial.html "]tutorial on regular expressions along with things like a [url=“http://www.regular-expressions.info/reference.html”]syntax reference for them.

yes, I use them also in my 4gl legacy app. programming language, which is called Powerhouse. Although I don’t know that it’s called a regular expression in that language, but typical code might look something like…
sele if matchpattern(gain_loss_code[1:2],“GA|AC|IA”) – so, very similar.

but “gain_loss_code” is just a data element; you don’t have to do anything to it first in order to check it, (or, in the above example, I’m checking the 1st two characters).

as a side note, just want to say that I really admire all you web programmers – when I started programming, 40 years ago, if I wanted to be a cobol programmer, I had to learn — cobol. that’s it. But programming the web? It seems to be almost like med school! You have to know html, css, javascript, sql_server, php, asp_net, and maybe vbscript and/or c# – and on and on. I don’t know how ya do it!