Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Regex help - probably an easy one for an expert.

Reply
Thread Tools

Regex help - probably an easy one for an expert.

 
 
DbZ
Guest
Posts: n/a
 
      08-07-2007
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

 
Reply With Quote
 
 
 
 
Evertjan.
Guest
Posts: n/a
 
      08-07-2007
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.

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


--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
 
Reply With Quote
 
 
 
 
Tim Streater
Guest
Posts: n/a
 
      08-07-2007
In article <(E-Mail Removed). com>,
DbZ <(E-Mail Removed)> wrote:

> 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.
 
Reply With Quote
 
Evertjan.
Guest
Posts: n/a
 
      08-07-2007
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.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
 
Reply With Quote
 
Tim Streater
Guest
Posts: n/a
 
      08-07-2007
In article <Xns99859DA9A7860eejj99@194.109.133.242>,
"Evertjan." <(E-Mail Removed)> wrote:

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




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.
 
Reply With Quote
 
Evertjan.
Guest
Posts: n/a
 
      08-07-2007
Tim Streater wrote on 07 aug 2007 in comp.lang.javascript:

> In article <Xns99859DA9A7860eejj99@194.109.133.242>,
> "Evertjan." <(E-Mail Removed)> wrote:
>
>> 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.

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

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      08-07-2007
DbZ wrote:
> 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
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f806at$ail$1$(E-Mail Removed)>
 
Reply With Quote
 
Dr J R Stockton
Guest
Posts: n/a
 
      08-07-2007
In comp.lang.javascript message <tim.streater-E51987.15031307082007@news
..individual.net>, Tue, 7 Aug 2007 15:03:13, Tim Streater
<(E-Mail Removed)> posted:
>In article <Xns99859DA9A7860eejj99@194.109.133.242>,
> "Evertjan." <(E-Mail Removed)> wrote:
>
>> 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.


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.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
For news:borland.*, use their server newsgroups.borland.com ; but first read
Guidelines <URL:http://www.borland.com/newsgroups/guide.html> ff. with care.
 
Reply With Quote
 
Evertjan.
Guest
Posts: n/a
 
      08-07-2007
Dr J R Stockton wrote on 07 aug 2007 in comp.lang.javascript:

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

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

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
 
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
How make regex that means "contains regex#1 but NOT regex#2" ?? seberino@spawar.navy.mil Python 3 07-01-2008 03:06 PM
(Probably) easy question about inheritance Zerex71 Java 19 08-30-2007 04:53 PM
probably an easy routing question, so please help pk Cisco 18 05-25-2007 03:15 PM
Probably an easy one for you but pls check johan.ekman@gmail.com C++ 6 05-27-2006 01:25 AM
Mental Block - probably easy answer...validation summary Rob Meade ASP .Net 3 11-25-2003 03:42 PM



Advertisments