regular expression

Discussion in 'Javascript' started by gaurav v bagga, Jan 20, 2007.

  1. hi all,

    i am finding it difficult to find a regular expression for following
    situation(replace)

    a b c d => a,b,c d
    a b c => a,b c

    i'll apreciate if anyone helps me with this...

    regards
    gaurav
     
    gaurav v bagga, Jan 20, 2007
    #1
    1. Advertising

  2. gaurav v bagga wrote:
    > i am finding it difficult to find a regular expression for following
    > situation(replace)
    >
    > a b c d => a,b,c d
    > a b c => a,b c
    >
    > i'll apreciate if anyone helps me with this...
    >


    alert("a b c".split(' ').join(',').replace(/(,)([^,]+)$/, ' $2'));


    JW
     
    Janwillem Borleffs, Jan 20, 2007
    #2
    1. Advertising

  3. gaurav v bagga

    Evertjan. Guest

    gaurav v bagga wrote on 20 jan 2007 in comp.lang.javascript:

    > i am finding it difficult to find a regular expression for following
    > situation(replace)
    >
    > a b c d => a,b,c d
    > a b c => a,b c
    >
    > i'll apreciate if anyone helps me with this...
    >


    You will have to explain better.

    Are a b c and d words, single letters, numbers?
    Do you mean any whitespave with your space,
    and replace all with a , but the last one??

    Try:

    t = t.replace(/\s+/g,',').replace(/,([^,]+)$/,' $1')

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., Jan 20, 2007
    #3
  4. Janwillem Borleffs wrote:
    > alert("a b c".split(' ').join(',').replace(/(,)([^,]+)$/, ' $2'));
    >


    Alternative, using look ahead assertion:

    alert("a b c".split(/\s(?![^\s]+$)/).join(', '));


    JW
     
    Janwillem Borleffs, Jan 20, 2007
    #4
  5. hi,

    thanks all for help it was very nice.

    regards
    gaurav
     
    gaurav v bagga, Jan 20, 2007
    #5
    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. Keith-Earl
    Replies:
    1
    Views:
    468
    Mary Chipman
    Jun 15, 2004
  2. VSK
    Replies:
    2
    Views:
    2,353
  3. =?iso-8859-1?B?bW9vcJk=?=

    Matching abitrary expression in a regular expression

    =?iso-8859-1?B?bW9vcJk=?=, Dec 1, 2005, in forum: Java
    Replies:
    8
    Views:
    872
    Alan Moore
    Dec 2, 2005
  4. GIMME
    Replies:
    3
    Views:
    12,024
    vforvikash
    Dec 29, 2008
  5. Noman Shapiro
    Replies:
    0
    Views:
    245
    Noman Shapiro
    Jul 17, 2013
Loading...

Share This Page