I have an array of strings with the following format
const references = [
'["C:\\Users\\user\\OneDrive\\Desktop\\PDES\\images\\fjJImt7Ym3pKvDCGzxwYL/fjJImt7Ym3pKvDCGzxwYL-0.png"',
'"C:\\Users\\user\\OneDrive\\Desktop\\PDES\\images\\fjJImt7Ym3pKvDCGzxwYL/fjJImt7Ym3pKvDCGzxwYL-1.png"]'
]
And I need to convert them into the following format
C:\Users\user\OneDrive\Desktop\PDES\images\fjJImt7Ym3pKvDCGzxwYL\fjJImt7Ym3pKvDCGzxwYL-1.png
Effectively doing the following
- Replacing all forward double slashes with one
- Removing the quotation marks
- Replacing the one backslash with a forward slash
- Removing the square brackets (those come from a
JSON.stringfy
in a different process)
To do this I've come up with the solution below
for (const imageRef of references ) {
let path = imageRef.split('"').join('').split('[').join('').split(']').join('').split('/').join("\\\\")
}
But the array could have more than 25 file references and I think the process could be better. Is there any way I can make this better? I did consider using .replaceAll()
but I'm working in Electron and that throws an error. I also took inspiration from this question but that was in 2010 and I'm sure things have changed.