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

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

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

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:
    554
    Mary Chipman
    Jun 15, 2004
  2. Antonio
    Replies:
    0
    Views:
    485
    Antonio
    Jan 19, 2005
  3. Replies:
    1
    Views:
    509
  4. lucky
    Replies:
    11
    Views:
    2,799
    Lucky
    Nov 11, 2005
  5. VSK
    Replies:
    2
    Views:
    2,685
  6. =?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:
    1,092
    Alan Moore
    Dec 2, 2005
  7. GIMME
    Replies:
    3
    Views:
    12,518
    vforvikash
    Dec 29, 2008
  8. Noman Shapiro
    Replies:
    0
    Views:
    434
    Noman Shapiro
    Jul 17, 2013
Loading...