//获取字符串中的url极其下标索引
function getHttpUrlArray(s) {
var s1 = s.match(/http.*/);
if(s1 == null) {
return null;
}
var yuan = s;
var url = [];
var i=0;
var indexArray = new Array(length);
while(s1 != null) {
var indexInL = { first: 0, last: 0, url: "" };
//获取下标索引
var index = s1.index;
//如果是第一个,加入
if(i == 0) {
indexInL.first = index;
} else {
indexInL.first = index; }
//查找之后的第一个汉字
//获取其下标索引
var s2 = s1[0].match(/[\u4e00-\u9fa5]/);
//若汉字存在
if(s2 != null) {
var noIndex = s2.index;
//如果是第一个,直接加入
if(i == 0) {
indexInL.last = noIndex + index;
} else {
indexInL.last = noIndex + index;
}
//取得url
indexInL.url = s1[0].substring(0, noIndex);
s = s.substring(noIndex + index);
s1 = s.match(/http.*/);
} else {
//如果是第一个,直接加入
if(i == 0) {
indexInL.last = yuan.length;
} else {
indexInL.last = yuan.length;
}
s = s.substring(index);
indexInL.url = s;
s1=null;
}
indexArray[i] = indexInL;
i++;
}
return indexArray;
}
比较笨,不会写正则表达式,只好用笨方法了。。。
有时候我们会复制带格式的内容,但是我们又需要将自己手写的网址添加上解析出来,下面的是将复制的url过滤,只返回手动添加的url
function getHttpUrlArray(s) {
var s0 = s.match(/http.*/);
var s1 = s.match(/[^'"=]http.*/);
console.log(s1)
var yuan = s;
var url = [];
var i = 0;
var indexArray =[];
var indexInL1 = { first: 0, last: 0, url: "" };
if(s0 != null) {
if(s0.index == 0) {
var s0end = s.match(/[\u4e00-\u9fa5<\s,,。;;]/);
if(s0end != null) {
indexInL1.first = 0;
indexInL1.last = s0end.index;
indexInL1.url = s.substring(0, s0end.index)
} else {
indexInL1.first = 0;
indexInL1.last = s.length;
indexInL1.url = s.substring(0, s.length)
}
indexArray[i] = indexInL1;
i++;
}
}
if(s1 == null && s0 == null) {
return null;
} else if(s1 == null && s0 != null) {
return indexArray;
}
while(s1 != null) {
console.log(s1)
var indexInL = { first: 0, last: 0, url: "" };
//获取下标索引
var index = s1.index;
indexInL.first = index + 1;
console.log("s1开始", index + 1)
//查找之后的第一个汉字或结束标签或,。;:
//获取其下标索引
var s2 = s1[0].substring(1).match(/[\u4e00-\u9fa5<\s,,。;;]/);
//若汉字存在
if(s2 != null) {
var noIndex = s2.index;
indexInL.last = noIndex + index + 1;
console.log("s2 结束", noIndex + index + 1)
//取得url
indexInL.url = s1[0].substring(1, noIndex + 1);
s = s.substring(noIndex + index + 1);
console.log("s的值 ", s)
s1 = s.match(/[^'"=]http.*/);
} else {
//如果是第一个,直接加入
if(i == 0) {
indexInL.last = s.length;
} else {
indexInL.last = s.length;
}
s = s.substring(index + 1);
indexInL.url = s;
console.log("url ", s)
s1 = null;
}
indexArray[i] = indexInL;
i++;
}
return indexArray;
}