skip to Main Content

For example i got that string 1500,,,4000,5000 i need to delete ,, commas, so this is came up with 1500,4000,5000

Another example: ,Banana,,Apple,,,,Watermelon, to became as clean as possible Banana,Apple,Watermelon

1

Answers


  1. A simple regular expression of ,{2,} will match any instance of 2 more more commas in a row. All matches of that can be replaced with a single comma.

    const str = '1500,,,4000,5000,,6000,,,,,,,,,,,,,,,7000';
    const pattern = /,{2,}/g; //finds 2 or more commas in a row
    const result = str.replace(pattern, ','); //replaces all matches with a singl comma
    
    console.log(result);
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search