Regex help - probably an easy one for an expert.

D

DbZ

Hi - i'm new to the regex thing - and trying to learn it to myself -
Can someone please explain to me what the following line does -
value.replace(/\s+$/g,"")

I can kinda figure out its replacing a something with an empty space -
on a global search. But thats about it

Thanks for your help
DBZ
 
E

Evertjan.

DbZ wrote on 07 aug 2007 in comp.lang.javascript:
Hi - i'm new to the regex thing - and trying to learn it to myself -
Can someone please explain to me what the following line does -
value.replace(/\s+$/g,"")

Please read the NG FAQ entry.
<http://www.jibbering.com/faq/#FAQ4_16>

However your string is not made by an expert,
because using the global flag "g" makes no sense,
when specifying the end of string "$" in the regex string.
 
T

Tim Streater

DbZ said:
Hi - i'm new to the regex thing - and trying to learn it to myself -
Can someone please explain to me what the following line does -
value.replace(/\s+$/g,"")

I can kinda figure out its replacing a something with an empty space -
on a global search. But thats about it

My advice is to steer clear of regex as much as you possibly can,
because anything more complex than the most simple examples are
incomprehensible about 5 mins after you last understood it.
 
E

Evertjan.

Tim Streater wrote on 07 aug 2007 in comp.lang.javascript:
My advice is to steer clear of regex as much as you possibly can,
because anything more complex than the most simple examples are
incomprehensible about 5 mins after you last understood it.

Bad advice, Tim.

As well as being terribly handy and enjoyable,
writing Regex is an important antidote
to Alzheimer progression.
 
T

Tim Streater

Evertjan. said:
Tim Streater wrote on 07 aug 2007 in comp.lang.javascript:


Bad advice, Tim.

As well as being terribly handy and enjoyable,
writing Regex is an important antidote
to Alzheimer progression.

:)

For some, perhaps. For me it's like line-noise, about as useful as
saying I should remember that shift-ctrl-alt-cmd-[ formats the third
partition on my disk.

During the 70s and the 80s there was TECO, an editor based on the notion
that typing more than one character to do something to your edit buffer
was a waste of effort. I put regex in the same category.
 
E

Evertjan.

Tim Streater wrote on 07 aug 2007 in comp.lang.javascript:
Evertjan. said:
Tim Streater wrote on 07 aug 2007 in comp.lang.javascript:


Bad advice, Tim.

As well as being terribly handy and enjoyable,
writing Regex is an important antidote
to Alzheimer progression.

:)

For some, perhaps. For me it's like line-noise, about as useful as
saying I should remember that shift-ctrl-alt-cmd-[ formats the third
partition on my disk.

Perhaps you are in need of such antidote? ;-{

During the 70s and the 80s there was TECO, an editor based on the
notion that typing more than one character to do something to your
edit buffer was a waste of effort. I put regex in the same category.

Like T9 and SMS-slang the youngsters are using nowayears?

No, I think Regex is a highly sophisticated sub language,
as it fills the gap that is left by higher languages
in providing string manipulation and testing.

The proficiency needed is on the level of boolean aritmethic,
that also is not inherent to javascript or basic/vbs/pascal/c++/etc.
 
T

Thomas 'PointedEars' Lahn

DbZ said:
Hi - i'm new to the regex thing - and trying to learn it to myself -

I can very much recommend reading the two free sample chapters of Mastering
Regular Expressions First and Second Edition by Jeffrey E. F. Friedl at
O'Reilly Online. And probably the book is worth buying.

http://www.oreilly.com/catalog/regex/ pp.
Can someone please explain to me what the following line does -
value.replace(/\s+$/g,"")

I can kinda figure out its replacing a something with an empty space -
on a global search. But thats about it

It is replacing one or more consecutive (`+') whitespace (`\s') at the end
(`$') of the string `value' with the empty string (`""', i.e. removing the
matched substring) and returns the result. (The `g'lobal flag which would
make the expression to apply to *every* match, not only the first one, is
therefore unnecessary here; there can be only one match because the
expression is anchored by `$'.)

For the above to have effect, you would need to assign the result to
something, for example:

value = value.replace(/\s+$/, "");


HTH

PointedEars
 
D

Dr J R Stockton

In comp.lang.javascript message <tim.streater-E51987.15031307082007@news
..individual.net>, Tue, 7 Aug 2007 15:03:13, Tim Streater

No : it does not make you keep your wits as you get much older, it
merely makes you feel much older while retaining the earlier wits, if
any.

But I agree - for any non-trivial (and some trivial) test manipulation,
use RegExps,

During the 70s and the 80s there was TECO, an editor based on the notion
that typing more than one character to do something to your edit buffer
was a waste of effort. I put regex in the same category.

I liked using TECO - one could write useful programs in it. I had
something which, at the start of an edit, would seek and update the date
and a version number; and something which would renumber cross-
references in numerical order.

TECO was better than the paper tape editor on the 905 - feed a command
tape, feed tape to be edited, punch new tape. But at least one had a
back-up to keep. At one stage, IIRC, I had so much editing to do on the
command tape that I made a command tape to edit the command tape.

I see - <http://almy.us/teco.html> - that TECO for WinXP etc. exists.

But I don't recall whether TECO had anything like RegExps.


I think PE (later) may be wrong - it could be the g which is wanted and
the $ which should go. If the replacement string were " ", that would
become likely.
 
E

Evertjan.

Dr J R Stockton wrote on 07 aug 2007 in comp.lang.javascript:
No : it does not make you keep your wits as you get much older, it
merely makes you feel much older while retaining the earlier wits, if
any.

John, you are pesimistic about the "use it or lose it" potential,

It is not decided yet.

Mental exercise [in the Morris water maze] really seems to have more than a
placebo effect:
"Voluntary Exercise Decreases Amyloid Load in a Transgenic Model of
Alzheimer's Disease" [The Journal of Neuroscience, April 27, 2005, 25
(17):4217-4221;]
<http://www.jneurosci.org/cgi/content/short/25/17/4217>

On the other hand:

Commentary: Activity each day keeps dementia away —
does social interaction really preserve cognitive function?
<http://ije.oxfordjournals.org/cgi/reprint/34/4/872>
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top