Get part of the url pathname via JavaScript regex
location.pathname.match(/\/article\/f\/(\d+)/)[1]
I'm trying to match /article/f/ and at least 1 digit captured by the group(note the parenthesis).
If that id is the single number in your path, you can get it directly by:
location.pathname.match(/\d+/)[0]
Regex URL Path from URL
This expression gets everything after videoplay
, aka the url path.
/\/(videoplay.+)/
This expression gets everything after the port. Also consisting of the path.
/\:\d./(.+)/
However If using Node.js
I recommend the native url
module.
var url = require('url')
var youtubeUrl = "http://video.google.co.uk:80/videoplay?docid=-7246927612831078230&hl=en#hello"
url.parse(youtubeUrl)
Which does all of the regex work for you.
{
protocol: 'http:',
slashes: true,
auth: null,
host: 'video.google.co.uk:80',
port: '80',
hostname: 'video.google.co.uk',
hash: '#hello',
search: '?docid=-7246927612831078230&hl=en',
query: 'docid=-7246927612831078230&hl=en',
pathname: '/videoplay',
path: '/videoplay?docid=-7246927612831078230&hl=en',
href: 'http://video.google.co.uk:80/videoplay?docid=-7246927612831078230&hl=en#hello'
}
How can I get a specific part of a URL using RegEx?
You could use the below regex:
[\d.]+(?=\.\w+$)
This matches dots and digits that are following a file extension. You could also make it more accurate:
\d+(?:\.\d+)*(?=\.\w+$)
Regex for extracting URL parts?
something like
var split = window.location.pathname.split(/\/|\?|&|=|\./g);
or if you want all in one string:
var classes = window.location.pathname.toLowerCase().replace(/\/|\?|&|=|\./g," ")
might do what you want
JavaScript regex to break an url into domain and path
Here is the breakdown javascript version. Hope this helps understand
//removes protocol
let regEx = /^(?:www\.)?(.*?):\/\//gim;
let url = "https://www.example.com/asdasd/123asdsd/sdasd?bar=1"
let path = url.replace(regEx, "");
console.log("path = " + path);
//removes domain extracts route
let regEx2 = /^(.*?\/)/;
if (path.match(regEx2)) {
let route = "/" + path.replace(regEx2, "");
console.log("route", route);
//extracts domain
url = path.match(regEx2);
let domainUrl = url[0].replace("/", "");
console.log("domainUrl = ", domainUrl);
}
Extracting url path using regexp
Are you saying trailing or leading slash? From your post it looks like leading slash.
document.location.pathname.replace(/^\//,"")
By the way, your regexp is right, but you just need to remove gi
and read matches[1]
rather than matches[0]
, because matches[0]
is the whole string matches the regexp, while matches[1]
is the captured part within the matched string (quote with brackets in the regexp).
var matches = document.location.pathname.match(/\/(.+)/);
console.log(matches); // ["/questions/ask", "questions/ask"]
Related Topics
How to Close Sweet Alert on Ajax Request Completion
Trying to Add Image to Pdf Using Jspdf
Filtering Item from Nested Object Array in Typescript
How to Set Page Size and Set Margin in Print in Internet Explorer With JavaScript and CSS
Webcam Js Error in Chrome: Could Not Access Webcam
Disable Click Outside of Bootstrap Modal Area to Close Modal
Jquery - Ajax Returning Error 500 on Some Posts
How to Instantiate a JavaScript Class in Another Js File
How to Get 30 Days Prior to Current Date
Get Image from Json File Using JavaScript and Display in HTML Img Tag
How to Add Query String Parameter to Same Url When on Click a Href Link
How to Change Font-Size in Vuetify Component
What Is the Purpose of the Dollar Sign in JavaScript
How to Prevent Invalid Characters from Being Typed into Input Fields
React Typescript Is Not Assignable to Parameter of Type
How to Wait Until 2 $Http Requests End in Angularjs
If a Div Contains a Specific String of Text, Edit the Parent'S CSS