Link to home
Start Free TrialLog in
Avatar of lulu50
lulu50Flag for United States of America

asked on

Remove double comma upon executing my Jquery code

Hi,

I have a textarea with the following list in it 736 4937 587 795 955

1. enter this list in a textarea 736 4937 587 795 955

2.. run my jquery: the output will be this   736, 4937, 587, 795, 955

   var str = $('#ItemList').val();
            str = $('#ItemList').val().replace(/(?:\r\n|\r|\n|\s)/g, ', ');
            $('#ItemList').val(str);

Open in new window



3.  now the user decide to run my jquery again: the user will get this output for running my query again   736,, 4937,, 587,, 795,, 955

4. How can I fix the jquery to say:

if the data in my textarea already has a comma and space between them then don't do anything.

only separate my list if there is no comma followed by a space between them.

I just don't wont the double comma if the user kept on clicking the button.


Thank you for your help,

Lulu50
Avatar of leakim971
leakim971
Flag of Guadeloupe image

   var str = $('#ItemList').val();
if( str.indexOf(", ") < 0) 
            str = $('#ItemList').val().replace(/(?:\r\n|\r|\n|\s)/g, ', ');

            $('#ItemList').val(str);

Open in new window

Avatar of lulu50

ASKER

leakim971

it looks good!!!

how can I say don't put comma on the last white space
so if I add a white space at the end of my list I'll get this

736, 4937, 587, 795, 955,

I want to say don't put any comma at the end of the list.




     var str = $('#ItemList').val();
            if (str.indexOf(",") < 0)
                str = $('#ItemList').val().replace(/(?:\r\n|\r|\n|\s)/g,', ');

            $('#ItemList').val(str);


ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lulu50

ASKER

leakim971


That is fabuleux my friend!!!!!

Thank you for all your help!!!!
Avatar of lulu50

ASKER

bravo comme toujours!!!

Merci beaucoup