469,327 Members | 1,335 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,327 developers. It's quick & easy.

Does anybody undesrstand this line of code from prototype.js

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 '06 #1
2 1334
eq******@gmail.com 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

Apr 19 '06 #2
eq******@gmail.com 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/
Apr 19 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by deejaybags | last post: by
14 posts views Thread by Anoop | last post: by
9 posts views Thread by pamelafluente | last post: by
14 posts views Thread by webEater | last post: by
92 posts views Thread by Heinrich Pumpernickel | last post: by
3 posts views Thread by kj | last post: by
19 posts views Thread by Prisoner at War | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Purva khokhar | last post: by
reply views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.