0

I want to remove empty commas from the string using JQuery. Please see the attached images to see what i mean.

enter image description here

enter image description here

2 Answers 2

2

This will remove all unnecessary commas in the text and update the text value:

$('.dyn-').text($('.dyn-').text().replace(/^,*|,(?=,)|,$/g, ''));

The regex is in three parts, seperated by |:

  1. first part catches all the commas in the beginning of the string, since they come before a word, they are all unnecessary,
  2. second part catches all commas followed by a comma, so only the last of the repeating commas will stay,
  3. and third part catches a comma that is at the very end of the string, which is unnecessary.
Sign up to request clarification or add additional context in comments.

2 Comments

could you please explain the regex?
@Anubhav i updated the answer to be more explanatory.
1

i think it would be easier to get to the source that is causing you this trouble instead of trying it backwards.. their is a solution but why not trying to remove it from the beginning?

any chance to let us see the code? maybe we could come up with the problem from the source.

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.