如何测试URL字符串是绝对字符串还是相对字符串-JavaScript?
要测试URL字符串,请使用正则表达式。
示例
以下是代码-
var regularExpressionForURL = /^https?:\/\//i;
var originalURL1 = "https://www.example.com/index";
var originalURL2 = "/index";
if (regularExpressionForURL.test(originalURL1)) {
console.log("This is absolute URL");
}
else {
console.log("This is relative URL");
}
if (regularExpressionForURL.test(originalURL2)) {
console.log("This is absolute URL");
}
else {
console.log("This is relative URL");
}要运行上述程序,您需要使用以下命令-
node fileName.js.
在这里,我的文件名为demo266.js
输出结果
这将在控制台上产生以下输出-
PS C:\Users\Amit\javascript-code> node demo266.js This is absolute URL This is relative URL
热门推荐
7 敏字的祝福语简短
10 生日油画棒祝福语简短
11 生日哥哥祝福语简短独特
12 高温早安祝福语简短精辟
13 朋友明天手术祝福语简短
14 冬季祝福语大全 简短的
15 对学姐的简短祝福语
16 收围巾的祝福语简短
17 送水杯祝福语女生简短
18 科研文案祝福语简短励志