-3

I am using a remote .txt file to populate an array which populates a drop-down select. It looks like this.

var myAnimalList = new Array();   
    function Food(anAnimal, aSpecies, anImage ) {

    this.Animal = anAnimal;
    this.Species = aSpecies;
    this.Image = anImage;
}

window.addEventListener("load", function(){

    document.getElementById("selAnimal").addEventListener("change", animalChosen);

    //big long string to parse
    var myAnimalData = InsectData2.txt;

    for (i = 0; i < AnimalDataLines.length; i++) {

        myAnimalData[i] = new Animals(subdata[0], subdata[1], subdata[2], subdata[3]);

      }     

However, my problem is. In the .txt file the image names contain spaces and slashes for example:

"Brown Skipper/Paratrylone/melane"

The image name on the other hand looks like this:

"brown-skipperpartrlonemelane.jpg" . But the image name

How can I ensure that the .txt file name matches the image name before I add it to the array?

Ele
  • 33,468
  • 7
  • 37
  • 75
  • seems like a simple task. https://stackoverflow.com/questions/2116558/fastest-method-to-replace-all-instances-of-a-character-in-a-string – epascarello Oct 02 '18 at 23:50

1 Answers1

4

What about a regex to match both

const initial = "Brown Skipper/Paratrylone/melane"
    // change space to dash (-)
    .replace(/\s/g, '-')
    // change slash to empty string
    .replace(/\//g, '')
    .toLowerCase()

const final = "brown-skipperparatrylonemelane.jpg".replace('.jpg', '')

initial === final
alexrqs
  • 183
  • 1
  • 6