有大概这样一个需求,请js高人指点
内容只允许aaa.com,bbb.com,ccc.aaa.com(二级域名)等三种指定域名开始的url,现在用户在提交表单时可能输入http://www.kkk.com,例如图片地址<img src="www.kkk.com/a.gif"/>,链接地址<a href="http://fff.cn">link</a>等,所有这些网址只要不匹配上面指定的三种域名开始法则则自动替换成http://www.aaa.com/
----------
举例下面一段文字:
----------
<img src="http://sina.cn/upload/a.gif"/>(替换)
hehhehehhehehhehehe
<a href="www.tom.com/blog/abc">(替换)请点击这里</a>
我的主页:http://www.bbb.com(没错)
----------
需要自动更正为:
----------
<img src="http://www.aaa.com/upload/a.gif"/>(替换)
hehhehehhehehhehehe
<a href="http://www.aaa.com/blog/abc">(替换)请点击这里</a>
我的主页:http://www.bbb.com(没错)-------------
上面括号列举的表示需要替换的地方,谢谢各位高人指点啊
内容只允许aaa.com,bbb.com,ccc.aaa.com(二级域名)等三种指定域名开始的url,现在用户在提交表单时可能输入http://www.kkk.com,例如图片地址<img src="www.kkk.com/a.gif"/>,链接地址<a href="http://fff.cn">link</a>等,所有这些网址只要不匹配上面指定的三种域名开始法则则自动替换成http://www.aaa.com/
----------
举例下面一段文字:
----------
<img src="http://sina.cn/upload/a.gif"/>(替换)
hehhehehhehehhehehe
<a href="www.tom.com/blog/abc">(替换)请点击这里</a>
我的主页:http://www.bbb.com(没错)
----------
需要自动更正为:
----------
<img src="http://www.aaa.com/upload/a.gif"/>(替换)
hehhehehhehehhehehe
<a href="http://www.aaa.com/blog/abc">(替换)请点击这里</a>
我的主页:http://www.bbb.com(没错)-------------
上面括号列举的表示需要替换的地方,谢谢各位高人指点啊
可以看看这个帖
String.prototype.Trim= function()
{
return this.replace(/^[\s ]*|[\s ]*$/g, "");
}function Replace() {
var ta = document.getElementById("ta");
var reg = /(<img(?:"[^"]*"|'[^']*'|[^"'>])+src=)(?!["']?http:\/\/www\.yi13\.cn\/)(?:"([^"]*)"|'([^']*)'|([^\s"'>]+))((?:"[^"]*"|'[^']*'|[^"'>])*>)/ig;
//"
alert("start");
ta.value = ta.value.Trim().replace(reg, "$1\"http://www.yi13.cn/bt/tu.asp?url=$2$3$4\"$5");
}
</script>
<textarea cols="136" rows="30" id="ta">
<P><IMG src="http://www.yi13.cn/bt/UpFile/UpAttachment/2008-1/2008111225715.jpg"></P>
<P><IMG src="http://www.163.com/2008111225715.jpg"></P>
<P><IMG height=640 alt=按此在新窗口浏览图片 src="http://blog.nnsky.com/photo/20082/37091/200821679321.JPG" width=480 onload=ResizeImage(this,560,1000) border=0></P>
<P><IMG height=420 alt=按此在新窗口浏览图片 src="http://blog.nnsky.com/photo/20082/37091/200821662038.JPG" width=560 border=0 ="ResizeImage(this,560,1000)"></P>
<P>></P>
</textarea>
<br/>
<input type='button' value='提交' onclick='Replace()'/>
写了一个,仍存在可优化的空间.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script>
function replaceUrl(){
o = document.getElementById("mytest")
str=o.value
///([ :"'=\/])(.*?(\.(?![(aaa)|(bbb)|(ccc)])(\..*?)))([ "'\/])/ig
// str = str.replace(/(.*?('|"|=|\s| )?)((http:\/\/)?(\w+\.)?(?!(a+|b+|test|est|st|t)\.)(\w+)(\.(cn|com|net)))(('| |")?(.*?))/ig,"$1http:\/\/www.aaa.com$10")
str = str.replace(/(.*?)((http:\/\/)?(\w+\.)*?(?!(a+|b+|test|est|st|t)\.)(\w+)(\.(cn|com|net)))(('| |")?(.*?))/ig,"$1http:\/\/www.aaa.com$10")
o.value=str
}
</script>
</head><body>
<table width="200" border="1">
<tr>
<td><label>
<textarea name="textarea" rows="20" cols="80" id=mytest><img src="http://sina.cn/upload/a.gif"/>(替换)
<img src="http://sina.com/upload/a.gif"/>(替换)
hehhehehhehehhehehe <img src="http://www.sina.com/upload/a.gif"/>(替换)
hehhehehhehehhehehe
<a href="www.tom.com/blog/abc">(替换)请点击这里 </a>
我的主页:http://www.test.bbb.com(没错)
我的主页:http://bbb.com(没错)
我的主页:http://test.com(没错)
我的主页:http://www.test1.com(替换)
www.test1.com(替换)
</textarea>
</label></td>
<td> </td>
<td><input type="button" name="cancle" value="替换" onclick="replaceUrl()"/></td>
</tr></table>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script>
function replaceUrl(){
o = document.getElementById("mytest")
str=o.value
///([ :"'=\/])(.*?(\.(?![(aaa)|(bbb)|(ccc)])(\..*?)))([ "'\/])/ig
// str = str.replace(/(.*?('|"|=|\s| )?)((http:\/\/)?(\w+\.)?(?!(a+|b+|test|est|st|t)\.)(\w+)(\.(cn|com|net)))(('| |")?(.*?))/ig,"$1http:\/\/www.aaa.com$10")
str = str.replace(/(.*?)((http:\/\/)?(\w+\.)*?(?!(a+|b+|test|est|st|t)\.)(\w+)(\.(cn|com|net)))(('| |")?(.*?))/ig,"$1http:\/\/www.aaa.com$10")
o.value=str
}
</script>
</head><body>
<table width="200" border="1">
<tr>
<td><label>
<textarea name="textarea" rows="20" cols="80" id=mytest><img src="http://sina.cn/upload/a.gif"/>(替换)
<img src="http://sina.com/upload/a.gif"/>(替换)
hehhehehhehehhehehe <img src="http://www.sina.com/upload/a.gif"/>(替换)
hehhehehhehehhehehe
<a href="www.tom.com/blog/abc">(替换)请点击这里 </a>
我的主页:http://www.test.bbb.com(没错)
我的主页:http://bbb.com(没错)
我的主页:http://test.com(没错)
我的主页:http://www.test1.com(替换)
www.test1.com(替换)
</textarea>
</label></td>
<td> </td>
<td><input type="button" name="cancle" value="替换" onclick="replaceUrl()"/></td>
</tr>
</table>
</body>
</html>
RegExp.Cache = {
/*---match URL---
* $1:protocol
* $2:hostname
* $3:port
* $4:path
* $5:pathname
* $6:hash
* $7:parameter
*/
URL:/^(https?|ftp|file):\/\/([^:\/]+)(?::(\d*))?\/?(?:(.*)\/)?([^#?]*)\#?([^?]*)(?:\?(.*))?$/
}
Browser.urlInfo = function(url)
{
if(RegExp.Cache.URL.test(url))
{
this.protocol = RegExp.$1;//协议http|ftp|https
this.hostName = RegExp.$2;//主机名www.xxx.com 也可以是IP地址
this.port = RegExp.$3;//端口号
this.path = RegExp.$4;//访问的文件路径 不包括文件名
this.fileName = RegExp.$5;//访问的文件名
this.hash = RegExp.$6;//锚点
this.param = RegExp.$7;//后面的参数字符串
//这个是根路径
this.rootUrl = [this.protocol,"://",this.hostName,(!this.port.isEmpty())?":"+this.port:""].join('');
//这个是相对路径
this.baseUrl = [this.rootUrl,this.path].join('/');
//数据的map 需要使用getParam方法获取 不过我没给出来
this._data = null;
}
else
return null;
}var ui = new Browser.urlInfo("http://www.163.com/xxx/aaa/sss.do#eee?param1=1¶m2=2");