17

削除しようとしています

http://localhost:7001/

からの一部

http://localhost:7001/www.facebook.com

出力を取得するには

www.facebook.com

この正確なパターンを実現するために使用できる正規表現は何ですか?

4

10 に答える 10

27

You don't need any library or REGEX

var url = new URL('http://localhost:7001/www.facebook.com')
console.log(url.pathname)

https://developer.mozilla.org/en-US/docs/Web/API/URL

于 2019-02-27T21:55:22.160 に答える
12

Based on @atiruz answer, but this is

url = url.replace( /^[a-zA-Z]{3,5}\:\/{2}[a-zA-Z0-9_.:-]+\//, '' );
  • shortest
  • can take https or ftp too
  • can take url with or without explicit port
于 2017-03-13T21:02:33.053 に答える
11

To javascript you can use this code:

var URL = "http://localhost:7001/www.facebook.com";
var newURL = URL.replace (/^[a-z]{4,5}\:\/{2}[a-z]{1,}\:[0-9]{1,4}.(.*)/, '$1'); // http or https
alert (newURL);

Look at this code in action Here

Regards, Victor

于 2012-07-18T22:50:39.800 に答える
5

This is how I made it work without resorting to regular expressions:

var URL = "http://localhost:7001/www.facebook.com";

var URLsplit = URL.split('/');

var host = URLsplit[0] + "//" + URLsplit[2] + "/";

var newURL = URL.replace(host, '');

Might not be an elegant solution though but it should be easier to understand for those who don't have much experience with regex (like me! ugh!).

于 2012-09-24T00:47:28.120 に答える
3

For a simple regex to match any protocol, domain, and (optionally) port:

var url = 'http://localhost:7001/www.facebook.com';

// Create a regex to match protocol, domain, and host
var matchProtocolDomainHost = /^.*\/\/[^\/]+:?[0-9]?\//i;

// Replace protocol, domain and host from url, assign to `myNewUrl`
var myNewUrl = url.replace(matchProtocolDomainHost, '');

Now myNewUrl === 'www.facebook.com'.

See demo on regex101

于 2017-06-26T01:15:04.090 に答える
2

All other regular expressions here look a bit complicated? This is all that's needed: (right?)

var originSlash = /^https?:\/\/[^/]+\//i;

theUrl.replace(originSlash, '');
于 2018-06-06T06:08:58.077 に答える
2

Regex to match the part of url, that you want to remove, will be something like: /^http[s]?:\/\/.+?\//

Example of Java code (note that in Java we use two backslashes "\\" for escaping character):

String urlWithBasePath = "http://localhost:7001/www.facebook.com";
String resultUrl = urlWithBasePath.replaceFirst("^http[s]?:\\/\\/.+?\\/", ""); // resultUrl => www.facebook.com

Example of JS code:

let urlWithBasePath = "http://localhost:7001/www.facebook.com";
let resultUrl = urlWithBasePath.replace(/^http[s]?:\/\/.+?\//, ''); // resultUrl => www.facebook.com

Example of Python code:

import re
urlWithBasePath = "http://localhost:7001/www.facebook.com"
resultUrl = re.sub(r'^http[s]?:\/\/.+?\/', '', urlWithBasePath) # resultUrl => www.facebook.com

Example or Ruby code:

urlWithBasePath = "http://localhost:7001/www.facebook.com"
resultUrl =  urlWithBasePath = urlWithBasePath.sub(/^http[s]?:\/\/.+?\//, '') # resultUrl => www.facebook.com

Example of PHP code:

$urlWithBasePath = "http://localhost:7001/www.facebook.com";
$resultUrl = preg_replace('/^http[s]?:\/\/.+?\//', '', $urlWithBasePath); // resultUrl => www.facebook.com

Example of C# code (you should also specify using System.Text.RegularExpressions;):

string urlWithBasePath = "http://localhost:7001/www.facebook.com";
string resultUrl = Regex.Replace(urlWithBasePath, @"^http[s]?:\/\/.+?\/", ""); // resultUrl => www.facebook.com
于 2020-06-15T08:50:34.733 に答える
1

または、 as3corelibURIクラスを使用してURLを解析することもできます。そうすれば、文字列を操作する必要がなくなり、意図しない想定を避けることができます。さらに数行のコードが必要ですが、さまざまなケースで機能する、より一般的なソリューションです。

var url : URI = new URI("http://localhost:7001/myPath?myQuery=value#myFragment");

// example of useful properties
trace(url.scheme); // prints: http
trace(url.authority); // prints the host: localhost
trace(url.port); // prints: 7001
trace(url.path); // prints: /myPath
trace(url.query); // prints: myQuery=test
trace(url.fragment); // prints: myFragment

// build a new relative url, make sure we keep the query and fragment
var relativeURL : URI = new URI();
relativeURL.path = url.path;
relativeURL.query = url.query;
relativeURL.fragment = url.fragment;

var relativeURLString : String = relativeURL.toString();

// remove first / if any
if (relativeURLString.charAt(0) == "/") {
    relativeURLString = relativeURLString.substring(1, relativeURLString.length);
}

trace(relativeURLString); // prints: myPath?myQuery=test#myFragment
于 2012-07-18T23:21:17.247 に答える
1

instead of using regex you could just use the browser's capabilities of parsing an URL:

var parser = document.createElement('a');
parser.href = "http://localhost:7001/www.facebook.com";
var path = parser.pathname.substring(1); // --> results in 'www.facebook.com'
于 2018-11-12T14:38:38.553 に答える
-5

Just use replace

"http://localhost:7001/www.facebook.com".replace("http://localhost:7001/",'')
于 2012-07-18T21:43:47.033 に答える