0

I have the below variable in JavaScript. I want to remove the "comma" sign using jQuery /JavaScript.

var test = ",,2,4,,,3,,3,,,"

Expected output: var test = "2,4,3,3"

please advice

orangetime
  • 55
  • 1
  • 6
  • 1
    Have you attempted to solve this problem? If you have, include your code and research in your question to show what hasn't worked for you. If not, you should attempt to solve it yourself first and then post the code and research here. It makes your question easier for others to answer too! – SuperBiasedMan Aug 05 '15 at 11:21
  • 2
    https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/replace & https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions – Arun P Johny Aug 05 '15 at 11:22
  • 4
    `test.match(/[^,]+/g).join()` – georg Aug 05 '15 at 11:27

1 Answers1

1

Use replace() with regex

var test = ",,2,4,,,3,,3,,,";

document.write(test.replace(/,+/g,',').replace(/^,|,$/g,''));
  1. replace(/,+/g,',') - To remove multiple comma with one.
  2. replace(/^,|,$/g,'') - To remove comma at starting and ending.
SuperBiasedMan
  • 9,814
  • 10
  • 45
  • 73
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188