This question already has answers here:
How can I get file extensions with JavaScript?
(36 answers)
How to extract extension from filename string in Javascript? [duplicate]
(10 answers)
Closed 2 years ago.
I'm allowing users to upload a file, however I need to check if the file has a proper name (since using multiple '.' characters throws off the .split() method). Here's what I have so far:
//Check if filename has more than 1 period character in the whole thing.
if(this.file.name.???){
this.fileNameAccepted = true;
} else {
this.fileNameAccepted = false;
}
let fileNameSplit = this.file.name.split('.');
The .split() method returns another array element for each '.' it finds. So fileNameSplit will have two elements if there are no '.' characters in the filename (there is automatically a '.' at the end of a filename before the file extension, ie hello-world.jpg, where it will always split a filename). A filename with 2 periods in the name (and 1 before the extension) will return 4 elements.
Use fileNameSplit[fileNameSplit.length - 1] to determine the last element (the file extension).
Related
This question already has answers here:
Remove a character at a certain position in a string - javascript [duplicate]
(8 answers)
How can I remove a character from a string using JavaScript?
(22 answers)
Closed 18 days ago.
I'm trying to do a calculation with time and need to remove the ":" which splits hours and minutes.
My array currently holds a string value of "12:04"
I created a for loop to iterate through the second array string by length, check for a :, then remove that character and log the new output. However, my logic is not working as intended. If you can, please let me know what I did wrong so I can fix my issue.
for (let i = 0; i < content[2].length; i++) {
if (content[2].charAt(i) === ":"){
content[2].slice(i);
console.log(content[2])
}
}
If you are sure that ":" will appear only once, then keep it simple
content[2] = content[2].replace(":", "");
Full code:
const result = content.map(str => str.replace(":", ""))
I think this works well:
content.split(':').join('')
Here is the output I got from the console:
"12:04".split(':').join('')
'1204' // Output
This question already has answers here:
Getting just the filename from a path with JavaScript
(7 answers)
Closed 3 years ago.
Currently with my code:
window.location.pathname.split('/')[3]
I can get "comparative" from:
http://localhost/study/78/comparative
But it's possible that some subdirectories will be added to the URI.
How can I get the last substring (the most to the right) on the URI separated with / ?
Try with slice
window.location.pathname.split('/').slice(-1)[0]
console.log("http://localhost/study/78/comparative".split('/').slice(-1)[0])
window.location.pathname.split('/').slice(-1)[0]
or
window.location.pathname.split('/').pop()
or
window.location.pathname.substr(window.location.pathname.lastIndexOf("/") + 1)
This question already has answers here:
How can I get file extensions with JavaScript?
(36 answers)
Regular expression to remove a file's extension
(9 answers)
How to trim a file extension from a String in JavaScript?
(27 answers)
Closed 4 years ago.
I have to parse strings that present name of files as:
myfilename.txt
anotherfilename.jpg
a.filename.with.dots.jpg
I need to extract the filename without it's extension.
for example "myfilename.txt" should return "myfilename"
and "a.filename.with.dots.jpg" should return "a.filename.with.dots"
not that some filename are problematic in what they contain and the javascript should be able to handle this.
what's the best was of doing this?
this is what I tried so far and didn't work.
namearr = name.split('.')
filename = namearr.splice()
I can use regex like
^(.?)..?$
for this but it seems like an overkill for this
the problem I see with other answered for this is that they don't take in account the number of dots the filename can contain. (not excluding the extension)
This question already has answers here:
How to check whether a string contains a substring in JavaScript?
(3 answers)
Closed 5 years ago.
I need JavaScript algorithm that can match substring of a sting?
subStringFinder('abbcdabbbbbck', 'ab')
should return index 0
and
subStringFinder('abbcdabbbbbck', 'bck') should return index 10
Could you please tell me how to write this code?
--EDIT:
Thanks to #Jonathan.Brink I wrote that code and it did the trick:
function subStringFinder(str, subString) {
return str.indexOf(subString);
}
subStringFinder('abbcdabbbbbck', 'bck') // -> 10
You are looking for the indexOf function which is available via the built-in string type (as well as array).
Example:
var str = "abbcdabbbbbck";
var n = str.indexOf("bck");
// n is 9
Probably, rather than having a custom subStringFinder function it would be better to just use indexOf.
This question already has answers here:
Finding the nth occurrence of a character in a string in javascript
(6 answers)
Closed 9 years ago.
I have a JavaScript variable which contains something like "fld_34_46_name". I need to be able to find the location of the THIRD _. The numbers, and name are not always the same (so, the string might also look like "fld_545425_9075_different name_test").
Is this possible? How could I do it?
Use the indexOf method three times:
var i = s.indexOf('_');
i = s.indexOf('_', i + 1);
i = s.indexOf('_', i + 1);
Note: If the string might contain fewer than three underscores, you would need to check for -1 after each time.