E
E
Evgeny Zhurov2018-02-07 16:43:34
JavaScript
Evgeny Zhurov, 2018-02-07 16:43:34

What regular expression can be used to cut such a string?

Good afternoon. There is a line:
"http://localhost:8080/somepath/somepath/somepath"
You need to get the part up to the third slash inclusive:
"http://localhost:8080/"
The line can change, i.e. url may change. for example:
"http://mysite.ru/"
And the regular expression should be able to process it too, so I decided that it would be advisable to get the part of the url before the third slash, because this option will work for any url.
JS code. I think it would be best to handle this matter through replace (), but I'm not strong in regular expressions. How to solve the problem correctly?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Stalker_RED, 2018-02-07
@Zhuroff

There is also a wonderful built-in parser.

let link = document.createElement('a');
link.href = "https://localhost8080/somepath/somepath/somepath?param=42#some-hash";

link.protocol // -> https:
link.host     // -> localhost8080
link.pathname // -> /somepath/somepath/somepath
link.hash     // -> #some-hash
link.search   // -> ?param=42
List of all properties here: https://developer.mozilla.org/ru/docs/Web/API/Location

M
Mikhail Osher, 2018-02-07
@miraage

url .
vde41

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question