SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2010
    Posts
    47
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question on this variable

    Hi,

    I have an example where the author does the following:

    function mouseOut() {
    var roll = new RegExp ("rollover");
    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

  2. #2
    SitePoint Enthusiast sffc's Avatar
    Join Date
    Jul 2006
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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:

    Code JavaScript:
    function mouseOut(){
    	if(this.id.match(/rollover/)){
    		// etc…
    "I haven't failed, I just found
    100,000 ways that don't work"
    Thomas Edison

  3. #3
    SitePoint Enthusiast
    Join Date
    Dec 2010
    Posts
    47
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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/))

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by jefals View Post
    What exactly is it doing that just saying "var roll = "rollout" isn't doing?
    It's assigning a regular expression to the variable.

    Quote Originally Posted by jefals View Post
    What tha heck is an object constructor?
    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.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Enthusiast
    Join Date
    Dec 2010
    Posts
    47
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by jefals View Post
    can you give me an example of an expression that is not a regular expression?)
    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.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  7. #7
    SitePoint Enthusiast
    Join Date
    Dec 2010
    Posts
    47
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  8. #8
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by jefals View Post
    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?
    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 tutorial on regular expressions along with things like a syntax reference for them.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  9. #9
    SitePoint Enthusiast
    Join Date
    Dec 2010
    Posts
    47
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Smile

    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!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •