Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Does anybody undesrstand this line of code from prototype.js

Reply
Thread Tools

Does anybody undesrstand this line of code from prototype.js

 
 
equalium@gmail.com
Guest
Posts: n/a
 
      04-19-2006
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.

 
Reply With Quote
 
 
 
 
Chris Diver
Guest
Posts: n/a
 
      04-19-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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/exp...s-javascript/2

HTH

Chris

 
Reply With Quote
 
 
 
 
Randy Webb
Guest
Posts: n/a
 
      04-19-2006
(E-Mail Removed) 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/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Read a file line by line and write each line to a file based on the5th byte scad C++ 23 05-17-2009 06:11 PM
<location> element does not seem to work - Does anybody know how to solve this. alexvodovoz@yahoo.com ASP .Net 1 05-25-2007 01:12 AM
How to read a text file line by line and remove some line kaushikshome C++ 4 09-10-2006 10:12 PM
Read a file line by line with a maximum number of characters per line Hugo Java 10 10-18-2004 11:42 AM
does anybody some code for this carl bloc C++ 1 05-25-2004 08:15 AM



Advertisments