removing file extension in strings in javascript [duplicate] - javascript

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)

Related

How to show below javaScript code without brackets? [duplicate]

This question already has answers here:
Easy way to turn JavaScript array into comma-separated list?
(22 answers)
Closed 2 years ago.
Code:
let myClassFellows=[ ['Noor',1],['Jawaria',2]];
console.log(myClassFellows[0]);
Below is the output I want to show without the square brackets
What do you mean without square brackets? Like 'Noor', 1?
If so, you need to concatenate it into a string with myClassFellows[0].join(', ').

Check if a string has multiple '.' characters [duplicate]

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).

Regular expression, get data from base64 [duplicate]

This question already has answers here:
How to strip type from Javascript FileReader base64 string?
(5 answers)
What do 'lazy' and 'greedy' mean in the context of regular expressions?
(13 answers)
Closed 3 years ago.
I have a base64 data
data:application/pdf;base64,JVBERi0xLj........
I want to get the rest of the data like
JVBERi0xLj........
Following not working, wonder what is the correct approach?
const express = /data:(.*?);base64,(.*?)/g;
const arr = express.exec(base64);
You can simplify and match all the data uri standard :
:([^;]*)[^,]+,(.*)
Your Regex will not match other data urls that are also standard like :
data:text/vnd-example+xyz;foo=bar;base64,R0lGODdh.
data:text/plain;charset=UTF-8;page=21,the%20data:1234,5678

Get the last substring separated with / [duplicate]

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)

Specific Regular Expression In Javascript with a condition [duplicate]

This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Parsing an URL in JavaScript
(3 answers)
Closed 8 years ago.
How to format a regular expression In JavaScript :
http://localhost:3000/sales?&tp=Home+Stay&am=Pool&pl=1620&pt=Flash+Sale&st=5
I want to get &pl= and all the digits after &pl=.
So the result would be &pl=1620 in this case.
Please help how to perform this??
Your help means a lot for me.
This script will do the trick:
var regex = /&pl=\d*/;
var match = regex.exec(yourtext)[0];
Following regex should do it
/&pl=\d*/.exec('http://localhost:3000/sales?&tp=Home+Stay&am=Pool&pl=1620&pt=Flash+Sale&st=5')[0]
Demo: Fiddel

Categories