Methods
(static) changeProtocol(url, protocol) → {string}
替换目标字符串中的 URL 协议。如果字符串中不包含协议,则加上协议。
Parameters:
Name | Type | Description |
---|---|---|
url |
string | 目标字符串。 |
protocol |
string | 协议。 |
Returns:
替换结果。
- Type
- string
Example
changeProtocol('abc.com', 'https'); // 'https://abc.com'
changeProtocol('http://abc.com', 'https'); // 'https://abc.com'
(static) startsWithProtocol(str, protocolsopt) → {boolean}
检查目标字符串是否以特定 URL 协议开头。
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
str |
string | 目标字符串。 | |
protocols |
Array |
<optional> |
特定协议(不含冒号和斜杠),不指定时表示允许任何协议。 |
Returns:
目标字符串是否以特定 URL 协议开头。
- Type
- boolean
Example
startsWithProtocol('//abc.com'); // true
startsWithProtocol('https://abc.com'); // true
startsWithProtocol('file:///Users/'); // true
startsWithProtocol('abc.com'); // false
startsWithProtocol('http://abc.com', ['http', 'https']); // true
startsWithProtocol('ftp://abc.com', ['http', 'https']); // false