Get Part of the Url Pathname Via JavaScript Regex

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



Leave a reply



Submit