代码问题
我有一个像 https://www.google.com
这样的字符串我想获得 www ,以便在//
之后和.
之前的字符串的子字符串(点)。
我该怎么办?
我尝试使用split
和substring
方法。
但这没用。
任何帮助都会很棒。
谢谢。
问题答案
使用replace()
let result = 'https://www.google.com'.replace(/https?:\/\/([^.]+).*/,'$1')
console.log(result)
,
看看URL浏览器API(如果可用)https://developer.mozilla.org/en-US/docs/Web/API/URL
另一个解决方案是
var parser = document.createElement('a');
parser.href = "http://example.com:3000/pathname/?search=test#hash";
parser.protocol; // => "http:"
parser.hostname; // => "example.com"
parser.port; // => "3000"
parser.pathname; // => "/pathname/"
parser.search; // => "?search=test"
parser.hash; // => "#hash"
parser.host; // => "example.com:3000"
如果觉得前端之家所整理的内容很不错的话,欢迎点击下方分享按钮,转发给身边开发程序员好友。