Does anybody undesrstand this line of code from prototype.js

Discussion in 'Javascript' started by equalium@gmail.com, Apr 19, 2006.

  1. Guest

    Hello, this sentence is from the Ajax.Request.prototype function in
    prototype.js:

    this.url += (this.url.match(/\?/) ? '&' : '?') + parameters;

    I understand what it does but I don't know how does "/\?/" work. Would
    it be the same as "this.url.match('?')" ?

    - What is each slash for?
    - Is "\?" an escape character?

    Can anybody help me?

    Carlos from Barcelona.
     
    , Apr 19, 2006
    #1
    1. Advertising

  2. Chris Diver Guest

    wrote:
    > Hello, this sentence is from the Ajax.Request.prototype function in
    > prototype.js:
    >
    > this.url += (this.url.match(/\?/) ? '&' : '?') + parameters;
    >
    > I understand what it does but I don't know how does "/\?/" work. Would
    > it be the same as "this.url.match('?')" ?
    >
    > - What is each slash for?
    > - Is "\?" an escape character?
    >
    > Can anybody help me?
    >
    > Carlos from Barcelona.
    >


    Hi Carlos

    Its a regular expression, broken down

    / is the start of the expression

    \? match a question mark anywhere in the url

    / is the end of the expresssion.

    the \ is an escape character because ? on its own means something
    else entirely.

    Regular expressions are a very powerful tool for matching patterns
    in strings. There is plenty of info on the web about them and
    one here.

    http://www.sitepoint.com/article/expressions-javascript/2

    HTH

    Chris
     
    Chris Diver, Apr 19, 2006
    #2
    1. Advertising

  3. Randy Webb Guest

    said the following on 4/19/2006 7:16 AM:
    > Hello, this sentence is from the Ajax.Request.prototype function in
    > prototype.js:


    It's a line of code, not a sentence. But another reason why I don't use
    prototype.js

    > this.url += (this.url.match(/\?/) ? '&' : '?') + parameters;
    >
    > I understand what it does but I don't know how does "/\?/" work. Would
    > it be the same as "this.url.match('?')" ?


    Close. It uses the ternary operator to see if it has a ? in the url and
    then appends parameters accordingly.

    > - What is each slash for?


    It is part of the Regular Expression that match is using.

    > - Is "\?" an escape character?


    Yes.

    --
    Randy
    comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
    Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
     
    Randy Webb, Apr 19, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    393
  2. June Lee
    Replies:
    2
    Views:
    808
    Jim Cobban
    Apr 13, 2008
  3. Replies:
    9
    Views:
    197
    Thomas 'PointedEars' Lahn
    May 26, 2006
  4. Replies:
    3
    Views:
    271
  5. javascript fish
    Replies:
    0
    Views:
    175
    javascript fish
    Oct 11, 2008
Loading...

Share This Page