Free NAS Free NAS - 3 months ago 7
jQuery Question

Remove in text jquery

I have problem with split or remove text from filename..

If I have like this filename:

200726100_50-0002.JPG

230514008_60-0001.JPG

Result must be:
230514008_60.JPG
200726100_50.JPG

Or Im not using correctly split function?

Answer

You can do it with a simple Array.map and regex like this:

// just add this function to your code

function getExpectedFilenames(filenames) {
  return filenames.map(function(filename) {
    var m = filename.match(/(\d{9}\_\d{2})-\d{4}(\.[A-z]+)/);
  
    if (m && m.length > 2) {
      return m[1] + m[2];
    } else {
      // or throw an error
      return filename;
    }
  });
}


// and use it like this:

var filenames = [
  '200726100_50-0002.JPG',
  '230514008_60-0001.JPG'
];

var expectedFilenames = getExpectedFilenames(filenames);

console.log("input filenames:", filenames);
console.log("output filenames:", expectedFilenames);


Or with simple Array.map and Array.split:

// just add this function to your code

function getExpectedFilenames(filenames) {
  return filenames.map(function(filename) {
    var s = filename.split('-');
    return s[0] +"."+ s[1].split('.')[1]
  });
}


// and use it like this:

var filenames = [
  '200726100_50-0002.JPG',
  '230514008_60-0001.JPG'
];


var expectedFilenames = getExpectedFilenames(filenames);

console.log("input filenames:", filenames);
console.log("output filenames:", expectedFilenames);