I'm trying to find a relatively easy and reliable method to extract the base URL from a string variable using JavaScript (or jQuery).
(我正在尝试找到一种相对简单可靠的方法,使用JavaScript(或jQuery)从字符串变量中提取基本URL。)
For example, given something like:
(例如,给出如下内容:)
http://www.sitename.com/article/2009/09/14/this-is-an-article/
I'd like to get:
(我想得到:)
http://www.sitename.com/
Is a regular expression the best bet?
(正则表达式是最好的选择吗?)
If so, what statement could I use to assign the base URL extracted from a given string to a new variable?(如果是这样,我可以使用什么语句将从给定字符串中提取的基本URL分配给新变量?)
I've done some searching on this, but everything I find in the JavaScript world seems to revolve around gathering this information from the actual document URL using location.host or similar.
(我已经对此进行了一些搜索,但我在JavaScript世界中找到的所有内容似乎都围绕着使用location.host或类似方法从实际文档URL收集此信息。)
ask by Bungle translate from so
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…