一个解析URL及图片地址的JS函数
类别: JavaScript教程
为BBS增加的解析函数,不知道有没有更好的方法,没看其它BBS的JS代码:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>BBS </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"/>
<SCRIPT LANGUAGE="JavaScript">
<!--
function prints(str) {
var matchPIC = new RegExp("((?:http|https|ftp|mms|rtsp)://(&(?=amp;)|[A-Za-z0-9./=?%_~@#:;+-])+(gif|jpg|png))", "ig");
var matchURL = new RegExp("((?:http|https|ftp|mms|rtsp)://(&(?=amp;)|[A-Za-z0-9./=?%_~@&#:;+-])+)","ig");
var str2 ="";
str2 =str.replace(" "," ")
if(matchPIC.test(str)){
document.write(str2.replace(matchPIC, "<img src="$1" hint="$1"></img>"));
}else{
document.write(str2.replace(matchURL, "<a target="_blank" href="$1">$1</a>"));
}
}
//-->
</SCRIPT>
</HEAD>
<BODY>
<SCRIPT LANGUAGE="JavaScript">
<!--
var str = "http://www.4ico.com/robot.jpg";
var str2 = "http://www.4ico.com/mail.png";
var str3 = "http://www.4ico.com";
document.write("<BR>str为"+str+"<BR>处理后为:<BR>");
prints(str);
str = "";
document.write("<BR>str2为"+str2+"<BR>处理后为:<BR>");
prints(str2);
str2= "";
document.write("<BR>str3为"+str3+"<BR>处理后为:<BR>");
prints(str3);
//-->
</SCRIPT>
</BODY>
</HTML>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>BBS </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"/>
<SCRIPT LANGUAGE="JavaScript">
<!--
function prints(str) {
var matchPIC = new RegExp("((?:http|https|ftp|mms|rtsp)://(&(?=amp;)|[A-Za-z0-9./=?%_~@#:;+-])+(gif|jpg|png))", "ig");
var matchURL = new RegExp("((?:http|https|ftp|mms|rtsp)://(&(?=amp;)|[A-Za-z0-9./=?%_~@&#:;+-])+)","ig");
var str2 ="";
str2 =str.replace(" "," ")
if(matchPIC.test(str)){
document.write(str2.replace(matchPIC, "<img src="$1" hint="$1"></img>"));
}else{
document.write(str2.replace(matchURL, "<a target="_blank" href="$1">$1</a>"));
}
}
//-->
</SCRIPT>
</HEAD>
<BODY>
<SCRIPT LANGUAGE="JavaScript">
<!--
var str = "http://www.4ico.com/robot.jpg";
var str2 = "http://www.4ico.com/mail.png";
var str3 = "http://www.4ico.com";
document.write("<BR>str为"+str+"<BR>处理后为:<BR>");
prints(str);
str = "";
document.write("<BR>str2为"+str2+"<BR>处理后为:<BR>");
prints(str2);
str2= "";
document.write("<BR>str3为"+str3+"<BR>处理后为:<BR>");
prints(str3);
//-->
</SCRIPT>
</BODY>
</HTML>
- 上一篇: 保存全部页面的方法
- 下一篇: 用JAVASCRIPT连接ACCESS数据库的方法
-= 资 源 教 程 =-
文 章 搜 索