Get 5 Characters After Last Slash
You could match until the last occurrence of a /
with a capturing group and a quantifier {1,6}
to match no
or flaged
Note that flaged
are 6 characters instead of 5 after the /
This page shows how to get the capturing group from the match using either REGEXP_SUBSTR or REGEXP_REPLACE.
^.*/([^/]{1,6})[^/]*$
Explanation
^.*/
Make sure to match the last occurrence of/
(
Capture group 1[^/]{1,6}
Match 6 times any char other than/
)
Close group 1[^/]*
Match 0+ occurrences of any char except/
$
End of string
See a regex demo
Regular Expression for getting everything after last slash
No, an ^
inside []
means negation.
[/]
stands for 'any character in set [/]'.
[^/]
stands for 'any character not in set [/]'.
How to grab the alphanumeric id at the end of a URL after the last forward slash in regex
Seems like some kind of hash based on base64 or md5 (too long for md5), so it can contain a-f or 0-9. I added the \s*
, because the third test contains space at the end. But if it was a mistake, just remove the \s*
from the expression.
([a-fA-F0-9]+)\s*$
https://regexr.com/604cq
Or to be more exact:
[=\/]([a-fA-F0-9]+)\s*$
If it can contain more characters, you can extend it to:
[=\/]([a-zA-Z0-9]+)\s*$
Grab the end of a URL after the last slash with regex in javascript
The simple method is to use a negated character class as
/[^\/]*$/
Regex Demo
Example
var referrerURL = "http://subdomain.xx-xxxx-x.xxx.url.com/content/assets/750";
alert(referrerURL.match(/[^\/]*$/));
// Output
// => 750
Replace everything after last slash in URL
Try this:
Search for: (.*/)(.*)$
Replace with:: $1authcode123$2
Explanation:
(.*) - Anything inside two parens "()" is called a capture
group. The text that matches the pattern inside the
first set of parens is stored in "capture group 1".
You can use $1 to refer to this capture group when
you do your replace. Likewise $2 will refer to the
second capture group.
. - A period represents ANY CHARACTER.
.* - The asterisk "*" means "zero of more of the precedding
pattern." So .* would mean "zero or more occurrences
of ANY CHARACTER."
(.*/) - zero or more characters followed by a slash
(the results of this capture are stored in $1)
(.*)$ - zero or more chars followed by the end of the string
(the results of this capture are stored in $2)
However, you haven't shared all of the details of your requirements, so I don't know if this answer is complete.
For example, what would you do for a url that has no slashes?
Regex to catch everything after last '/' in URL
Use components(separatedBy:)
and get the last
component from it, i.e.
url1.components(separatedBy: "/").last //"exmaple1.jpg"
url2.components(separatedBy: "/").last //"11974343961963318467fl.png"
url3.components(separatedBy: "/").last //"01+Seth+Easy+v1.gif"
Or you can use lastPathComponent
on the URL
instance created using url1, url2 and url3
URL(string: url1)?.lastPathComponent //"exmaple1.jpg"
URL(string: url2)?.lastPathComponent //"11974343961963318467fl.png"
URL(string: url3)?.lastPathComponent //"01+Seth+Easy+v1.gif"
All characters after last slash JavaScript RegEx
Try this code:
var string = 'http://steamcommunity.com/id/user/'
document.body.innerHTML = string.match(/([^/]+)\/$/)[1]
I added a capturing group to capture the user folder.
Since that was the 1st
capturing group, I selected the item at index 1
of the returned array from string.match()
var string = 'http://steamcommunity.com/id/user/'
document.body.innerHTML = string.match(/([^/]+)\/$/)[1]
How to get number after last slash using regex?
You can use this lookahead based regex:
\/([0-9]+)(?=[^\/]*$)
Online Demo: http://regex101.com/r/eL3mK3
This means find a number which is followed by 0 or more non-slash string till end.
Get all characters after the last '/' (slash) from url string
For Swift 3 try this.
let fileName = "your/file/name/here"
let fileArray = fileName?.components(separatedBy: "/")
let finalFileName = fileArray?.last
Output : "Here"
Related Topics
While Looping a Jquery Ajax Call
Ask for Geolocation Permission Again If It Was Denied
Prevent CSS Tooltip from Going Out of Page/Window
Exclude a Folder from Glob Pattern Matching
Webaudio - Seamlessly Playing Sequence of Audio Chunks
How to Use a Function from Another File in React
How to Stop Page Getting Refresh on Validating Form
Finding Sequential Values in Js Array
Vuetify: How to Preselect Active Tab
How to Generate Event Handlers With Loop in JavaScript
How to Highlight a Part of Text in Textarea
After Clicking a Button of Sweet Alert Call PHP File
How to Prevent Browser to Re-Set Inputs on Page Refresh
Chart.Js Showing Time (Hh:Mm:Ss - 24 Hour Clock) on Xaxis
Sequelize - How to Return Json Objects of the Database Results Only
Why Am I Getting Undefined as an Answer When Fetching Data from an API
Combine Array of Objects With Similar Keys Conditionally Using JavaScript