How do I use a regex in JavaScript to extract specific parts of a URL path?
NickName:user11039951 Ask DateTime:2021-02-03T00:20:02

How do I use a regex in JavaScript to extract specific parts of a URL path?

Right now, I'm trying to take a URL in this format:

https://www.example.com/{section}/posts/{number}

and get section and number. I need to do it with a regex; I cannot just break it into a parts array. I have tried:

var sect = myURL.match('https://www.example.com/[^/]+');

but I get as output "https://www.example.com/{section}". I want to be able to get the section and the number. How do I do this in JavaScript?

Copyright Notice:Content Author:「user11039951」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/66013555/how-do-i-use-a-regex-in-javascript-to-extract-specific-parts-of-a-url-path

Answers
anubhava 2021-02-02T16:42:22

You can assign output of matches to multiple variables like this:\n\r\n\r\nvar myURL = 'https://www.example.com/mysection/posts/1234';\n\n[$0, sec, num] = myURL.match(/^https?:\\/\\/www\\.example\\.com\\/([^\\/]+)\\/posts\\/(\\d+)\\/?$/);\n\nconsole.log(sec)\n//=> mysection\n\nconsole.log(num)\n//=> 1234",


MonkeyZeus 2021-02-02T16:26:36

If you don't have to validate that the string is in fact a URL then just split it on forward slashes.\n\r\n\r\nvar parts = `https://www.example.com/{section}/posts/{number}`.split(/\\//);\nconsole.log(parts[3]);\nconsole.log(parts[5]);",


More about “How do I use a regex in JavaScript to extract specific parts of a URL path?” related questions

How do I use a regex in JavaScript to extract specific parts of a URL path?

Right now, I'm trying to take a URL in this format: https://www.example.com/{section}/posts/{number} and get section and number. I need to do it with a regex; I cannot just break it into a parts ar...

Show Detail

How to extract part of an URL path in regex

In my url that might look like this: protocol://server/path1/path2/path3/pathn/key/id1234567890?Create I want to get the value id1234567890 (via a group) but I don't know how many "path" parts th...

Show Detail

Extract specific URL parts with regex and rewrite

I'm trying to rewrite some urls and extract their parameter values via path names via one regular expression. The url can have multiple variations and s-term, pg-n, sort-order and so on can be in any

Show Detail

Extract parts of a URL

Basically I am looking to extract a URL until it encounters a number which may or may not be present Examples: http://www.test.com/products/cards/product_code/12345/something_else http://www.tes...

Show Detail

Regex for extract for url path

I need help with creating a regex for PostgresSQL to extract specific url paths and place them into a separate column. For example, using the following URLs I need to extract https://example.com/

Show Detail

JavaScript regex to extract different parts of a string

This is an interesting one - I am looking for a JavaScript regex solution to extract different parts from a string. Any input is much appreciated. Example - ";1XYZ123_UK;1;2.3;evt14=0.0|evt87=0...

Show Detail

Regex - Parsing Parts of a URL in JMeter

I am trying to parse parts of the URL that I received from an API. I am trying to do this using Regex in Jmeter and save it into Variables. I have a URL which looks like I receive a URL as part of ...

Show Detail

Getting parts of a URL (Regex)

Given the URL (single line): http://test.example.com/dir/subdir/file.html How can I extract the following parts using regular expressions: The Subdomain (test) The Domain (example.com) The path w...

Show Detail

Getting parts of a URL (Regex)

Given the URL (single line): http://test.example.com/dir/subdir/file.html How can I extract the following parts using regular expressions: The Subdomain (test) The Domain (example.com) The path w...

Show Detail

Regex to extract parts of string

I'm setting up some campaign codes which will appear as a query parameter in a URL. I'd like to automate the reporting of these campaign codes and have set them up in such a way that each parameter

Show Detail