I'm trying to use JavaScript to transform this string
.txt|.pdf|.xls|.xlsx|.doc|.docx|.rtf|.jpg|.jpeg|.png|.gif
into this array
["txt", "pdf", "xls", "xlsx", "doc", "docx", "rtf", "jpg", "jpeg", "png", "gif"]
But it gives me this
[".txt", "pdf", "xls", "xlsx", "doc", "docx", "rtf", "jpg", "jpeg", "png", "gif"]
It keeps the dot in front of first element. What can I do since I don't know regex? Here is my code:
let fileTypes = string.split('|.');
The problem only seems to be the first dot, so you could just do
s = ".txt|.pdf|.xls|.xlsx|.doc|.docx|.rtf|.jpg|.jpeg|.png|.gif"
s.substr(1).split("|.")
Something like this:
var string = '.txt|.pdf|.xls|.xlsx|.doc|.docx|.rtf|.jpg|.jpeg|.png|.gif';
var arr = string.replace(/\./g,'').split('|');
This will first strip all of the dots, then split on the |
. Separating by the pipe is all that matters... So it will take a more flexible string if that matters.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With