Regex help - probably an easy one for an expert.

Discussion in 'Javascript' started by DbZ, Aug 7, 2007.

  1. DbZ

    DbZ Guest

    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
     
    DbZ, Aug 7, 2007
    #1
    1. Advertising

  2. DbZ

    Evertjan. Guest

    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)
     
    Evertjan., Aug 7, 2007
    #2
    1. Advertising

  3. DbZ

    Tim Streater Guest

    In article <>,
    DbZ <> 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.
     
    Tim Streater, Aug 7, 2007
    #3
  4. DbZ

    Evertjan. Guest

    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)
     
    Evertjan., Aug 7, 2007
    #4
  5. DbZ

    Tim Streater Guest

    In article <Xns99859DA9A7860eejj99@194.109.133.242>,
    "Evertjan." <> 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.
     
    Tim Streater, Aug 7, 2007
    #5
  6. DbZ

    Evertjan. Guest

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

    > In article <Xns99859DA9A7860eejj99@194.109.133.242>,
    > "Evertjan." <> 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)
     
    Evertjan., Aug 7, 2007
    #6
  7. 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$>
     
    Thomas 'PointedEars' Lahn, Aug 7, 2007
    #7
  8. In comp.lang.javascript message <tim.streater-E51987.15031307082007@news
    ..individual.net>, Tue, 7 Aug 2007 15:03:13, Tim Streater
    <> posted:
    >In article <Xns99859DA9A7860eejj99@194.109.133.242>,
    > "Evertjan." <> 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.
     
    Dr J R Stockton, Aug 7, 2007
    #8
  9. DbZ

    Evertjan. Guest

    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)
     
    Evertjan., Aug 8, 2007
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rob Meade
    Replies:
    3
    Views:
    2,351
    Cowboy \(Gregory A. Beamer\)
    Nov 25, 2003
  2. Replies:
    6
    Views:
    339
    Rolf Magnus
    May 27, 2006
  3. Zerex71
    Replies:
    19
    Views:
    577
    Patricia Shanahan
    Aug 30, 2007
  4. middletree

    probably easy for you, but . . .

    middletree, Jul 28, 2003, in forum: ASP General
    Replies:
    3
    Views:
    104
    middletree
    Jul 28, 2003
  5. Eric
    Replies:
    4
    Views:
    210
    Ray at
    Sep 24, 2003
Loading...

Share This Page