Does anybody undesrstand this line of code from prototype.js

E

equalium

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.
 
C

Chris Diver

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
 
R

Randy Webb

(e-mail address 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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top