索引a“/"在字符串中
indexOf a "/ " in a string
我有一个HotSpot URL,如下所示:
http://192.168.2.251/XPages/Website/wfswebsite.nsf/xpIndex.xsp?page=ccProducts.xsp&product=ccWFAXPages.xsp
我试图从URL indexOf中提取192.168.2.251,返回/的第一次出现。我无法对192进行索引,因为在现实生活中,它可能是一个IP地址或一个实际的域名。
假设您能够使用一个函数,并且不受indexOf()
方法的任何原因的限制,我建议:
function getDomain(url) {
// create a temporary <a> element:
var a = document.createElement('a');
// set its `href` property (not attribute) to
// the given URL:
a.href = url;
// return the hostname of the created-<a> element:
return a.hostname;
}
var url = "http://192.168.2.251/XPages/Website/wfswebsite.nsf/xpIndex.xsp?page=ccProducts.xsp&product=ccWFAXPages.xsp",
hostname = getDomain(url);
document.getElementById('urlString').textContent = url;
document.getElementById('hostname').textContent = hostname;
ul, li {
margin: 0;
padding: 0;
}
li {
max-width: 90%;
}
li::before {
text-indent: 1em;
content: attr(id)": ";
display: block;
color: #696;
}
li:empty {
display: none;
}
<ul>
<li id="urlString"></li>
<li id="hostname"></li>
</ul>
参考文献:
- CCD_ 2
URLUtils
上面的建议让我寻找一些替代解决方案,事实上,这段代码正是我想要的:
var thisURL = context.getUrl();
var host:String = thisURL.getHost();
因为thisURL是一个xspURL,所以getHost在本例中提取192.168.2.251并返回我想要的内容。
相关文章:
- 内联javascript与"<脚本>"字符串错误地关闭了脚本标记
- 用分隔符分隔具有多个整数值的字符串的Javascript"重试错误的值
- 如何删除“"使用javascript从字符串中提取字符
- 解析字符串包含'"'从mysql检索到JSON
- 为什么我得到“;错误:预期"同时访问字符串
- 传递带有符号“”的字符串变量'"到函数
- 添加前缀“#"到数组中的某些字符串值
- 索引a“/"在字符串中
- 看到意想不到的"未定义的“;在Node JS提供的字符串中
- 用“”查找字符串//"并从“//"直到最后
- 替换正斜杠“/"JavaScript字符串中的字符
- 检查字符串是否只包含“0”-"什么都没有
- 带有换行符的JSP+HTML-将其作为字符串获取,并删除"新线”;
- Regex提取字符串后的第二个“"字符串末尾的点字符
- javascript exclude"00”;以及“;01〃;字符串
- jquery ui的客户端缓存自动完成:javascript/jquery regex匹配字符串"索引“;对象
- jQplot格式字符串"%n〃;
- 基于"字符串"的任意数组元素;种子
- 获取最后一个字符串“"其可以包含“&”;
- 匹配字符串“/$"在RegExp构造函数中