Skip to content

Latest commit

 

History

History
33 lines (21 loc) · 1.07 KB

is-absolute-url.md

File metadata and controls

33 lines (21 loc) · 1.07 KB
标题 标签
isAbsoluteURL(是否是绝对路径) string,browser,regexp(字符串,浏览器,正则表达式)

检查给定的字符串是否是绝对 URL。

  • 使用 RegExp.prototype.test() 测试字符串是否为绝对 URL。
const isAbsoluteURL = url => /^[a-z][a-z+.-]*:/.test(url);

ts 代码如下:

调用方式:

isAbsoluteURL('https://google.com'); // true
isAbsoluteURL('ftp://www.myserver.net'); // true
isAbsoluteURL('/foo/bar'); // false

应用场景

以下是一个实战示例:

结果如下:

<iframe src="codes/javascript/html/is-absolute-url.html"></iframe>