简体   繁体   中英

How to remove multiple comma from end of string using javascript?

This is my output

9781473507340.epub,9781473518902.epub,,,,,,

i need this output

9781473507340.epub,9781473518902.epub

only using dynamically get files name in javascript.It possible get dynamically

To remove every trailing comma, you could use this regex:

var str = "9781473507340.epub,9781473518902.epub,,,,,,";
var res = str.trim().replace(/,{1,}$/, '');
console.log(res); // 9781473507340.epub,9781473518902.epub

You might do as follows;

 var str = "9781473507340.epub,9781473518902.epub,,,,,,", newStr = str.replace(/,*(?=$)/,""); console.log(newStr); 

Could be an overkill, but you can try something like this.

These steps can be followed.

  1. Split to an array used split() based on the commas.
  2. Filter out the blank items.
  3. Put the new array back into to a string using join() .

 var input = '9781473507340.epub,9781473518902.epub,,,,,,'; var output = input .split(',') //split to array .filter(function(val, b){ return val.length }) //filter out the blank items .join(','); //put the new array to a string. console.log(output); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM