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)
Related
This question already has answers here:
How to parse a URL?
(6 answers)
Closed 8 months ago.
This is the format for which I want to generate regex https://<any>.blob.core.windows.net/<any>?<any> where any allowed any character(including special char also). I mean all where the other part is static.
Can you help to generate regex for above pattern?
Thanks
This will match anything in the places where you put <any>.
https:\/\/.*\.blob\.core\.windows\.net\/.*\?.*
/^https:\/\/.+\.blob\.core\.windows\.net\/.+\?.+$/gm
<any> - any character (except for line terminators) between one and unlimited times. Can't be empty
This question already has answers here:
Why do regex constructors need to be double escaped?
(5 answers)
Javascript Regex: How to put a variable inside a regular expression? [duplicate]
(9 answers)
Closed 2 years ago.
I created a RegExp that works as expected however I can't get it to work in a string format
var regex = new RegExp(/IN1\|2.*\r/g);
The regex is supposed to match the line number which will be taken from variable, in above example it would be line number 2. My question is how do I get it to a string format with a variable inside it?
I tried following but it just doesn't work: "IN1\|" + lineNumber + ".*\x0d//g"
Below is a text in case anyone wants to try:
IN1|**1**||QQ|Noth||||||||20190413|20190413||Self\r
IN1|**2**||QQ|Noth||||||||20190413|20190413||Self\r
IN1|**3**||QQ|LHS||||||||20200506|""||Private|||||||||||||||||||||2342344\r
Thank you.
This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Regex to first occurrence only? [duplicate]
(4 answers)
Closed 4 years ago.
I have a string
var str3 = "[a,b,c] there [s,b,c] how are u";
What i want is [there, how are u] but I am getting ["", "how are u"], when splitting with str3.split(/\[.*\]/);
Any idea how i shud do it?
You shouldn't be greedy:
str3.split(/\[.*?\]\s*/);
:-)
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 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