0

I am trying to replace 「.file extension,」 into 「,」

「1805171004310.jpg,1805171004311.png,1805171004312.jpg,」 into 「1805171004310,1805171004311,1805171004312,」

How can I make it lazy and repeat? https://jsfiddle.net/jj9tvmku/

dataArr = new Array();
dataArr[1] = '1805171004310.jpg,1805171004311.png,1805171004312.jpg,';

fileNameWithoutExt = dataArr[1].replace('/(\.(.*?),)/', ',');

$('#msg').val(fileNameWithoutExt);

https://regex101.com/r/nftHNy/3

AllenBooTung
  • 340
  • 2
  • 16

1 Answers1

4

Just use the global flag g.

Your regex, isn't actually a regex, it's a string. Remove the single quotes surrounding it: /(\.(.*?),)/g, And you can remove all the capture groups, since are not needed here: /\..*?,/g

const dataArr = new Array();
dataArr[1] = '1805171004310.jpg,1805171004311.png,1805171004312.jpg,';

const fileNameWithoutExt = dataArr[1].replace(/\..*?,/g, ',');
console.log(fileNameWithoutExt);
// or an array of filenames
console.log(fileNameWithoutExt.split(',').filter(Boolean));

If you want the file names individually, use .split(',').filter(Boolean)

Marcos Casagrande
  • 37,983
  • 8
  • 84
  • 98