Can someone explain how this javascript function works?

I am still new to javascript and want to understand what this means. It is supposed to remove leading & trailing spaces from a string. Can someone explain to me what it means/how it works? Thanks.

Amy

function trim(aStr) {

return aStr.replace(/^\s{1,}/, “”).replace(/\s{1,}$/, “”)

}

Subject: Can someone explain how this javascript function works?

The funky looking character strings /^\s{1,}/ are called “Regular Expressions” (patterns that match portions of strings) very powerful javascript mojo…

Ex 1. /^\s{1,}/

/…/ -->Encloses the regular expression

^ → look at the beginning of the string

\s → space

{1,} → one or more

So, this regex says look for one or more spaces at the beginning of aStr.

=====================

Ex 2. /\s{1,}$/

/…/ -->Encloses the regular expression

\s → space

{1,} → one or more

$ → look at the end of the string

So, this regex says look for one or more spaces at the end of aStr.

The replace part says replace them with nothing “”

A good quick ref can be found here.

http://visibone.com/regular-expressions/

hth

regards

Subject: RE: Can someone explain how this javascript function works?

Excellent explanation! Thanks!