1

I have a string like

Andrew,Lucy,,David,Tess,

I want to remove ,, after Lucy and also a comma from the end of the string so the end results i need would be

Andrew,Lucy,David,Tess

is there any way to achieve this in jQuery?

4
  • .replace(/,,|,\s/,"") Commented Aug 20, 2016 at 6:42
  • Thanks zer00ne but it doesn't work. Here are the results AndrewLucy,,David,Tess, Commented Aug 20, 2016 at 6:48
  • I forgot the flag str.replace(/(,{2,})|(,$)/g, "") Commented Aug 20, 2016 at 6:56
  • 1
    "in jQuery" - jQuery is not a string manipulation library. (Which is why none of the answers used it.) Commented Aug 20, 2016 at 7:09

5 Answers 5

2

Use Positive Lookahead ?=

var str = "Andrew,Lucy,,David,Tess,";
str = str.replace(/,(?=,+|$)/g, "");
console.log( str ); // Andrew,Lucy,David,Tess

stop at comma , than lookahead ?= for it's followed by:
,+ one or more commas
| or it's at end of string $

Important

Most probably you'll need to take care for cases when the string starts with one or more commas, than simply append ^,+| to your regex:

var str = ",,Andrew,Lucy,,David,Tess,,";
str = str.replace(/^,+|,(?=,+|$)/g, "");
console.log( str ); // Andrew,Lucy,David,Tess

or respectively /^,+|,(?=,+)|,+$/g

Sign up to request clarification or add additional context in comments.

1 Comment

I think this is a perfect answer
2

This is a working regex:

/,+(?=,|$)/g

Online Demo

Remove redundant commas followed by a comma or end of line. If you apply this to multiple lines add m (multiline flag) after the closing slash too (otherwise $ matches only at the very end of the text instead of at each newline).

Code Demo

// Add /i at bottom to make the regex it case insensitive
var re = /,+(?=,|$)/g; 

var tests = ['Andrew,Lucy,,David,Tess,','Andrew,,,,Lucy,,,David,Tess,,,'].reverse();
var m;

while( t = tests.pop() ) {
    document.write('"' + t + '" => <font color="green">' + t.replace(re,'') + '</font><br/>');
}

2 Comments

What about ,,Andrew cases ? ;)
@RokoC.Buljan: I strictly follow the "Don't add unrequested feature" XP principle: :P
0

Thanks all. Really appreciate your efforts.

Did something like this. It worked

var your_string = 'Andrew, Lucy,, David, Tess,';
newstr=your_string.replace(',,', ',');

your_string1 = newstr.replace(/[,]$/,'');

first i removed double commas and then i removed comma from the end

4 Comments

Isn't ,+ bulletproofer than ,,,,,, ? I realize that the requirement was ,, but hey yo.
You can also chain it: var newstr=your_string.replace(',,', ',').replace(/[,]$/,'');
If you pass a string as the first argument to .replace() it will only replace the first match. Is that what you want?
The brackets in /[,]$/ are unnecessary. /,$/ will suffice. Not sure what are all the cases you need to support, but for more variants of , positions - this solution is insufficient
-1

Or You can simply use .replace()

var str = "Andrew,Lucy,,David,Tess,";
var res = str.replace(",,", ",").replace(/,\s*$/, ""); 

Working Demo

1 Comment

@DarrenSweeney : My bad. I did not notice that in question. done editing
-1

Without a regex :

var str = "Andrew,Lucy,,David,Tess,";
var res = str.split(",");
res = res.filter(Boolean);
res = res.join(",");
console.log(res);

With a regex and replace function():

var str = "Andrew,Lucy,,David,Tess,";
var res = str.replace(/,,|,$/g, function(x){
  return x == ",," ? ",": "";});
console.log(res);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.