<body>
<textarea id="txt"rows="10" cols="20">
</textarea>
<input type ="button" value ="测试" onclick ="_tests()"/>
</body>
<script language =javascript >
function _tests()
{
var txts=document .getElementById("txt").value ;
var context=(/^[h|f]tp:www.[a-zA-Z0-9]+.[com|cn]$/g);
while (res=context.exec(txts))
{
window .alert (res[0]);
}
}
</script>这个是匹配类似与www.baidu.com的url,结果为啥不对?
<textarea id="txt"rows="10" cols="20">
</textarea>
<input type ="button" value ="测试" onclick ="_tests()"/>
</body>
<script language =javascript >
function _tests()
{
var txts=document .getElementById("txt").value ;
var context=(/^[h|f]tp:www.[a-zA-Z0-9]+.[com|cn]$/g);
while (res=context.exec(txts))
{
window .alert (res[0]);
}
}
</script>这个是匹配类似与www.baidu.com的url,结果为啥不对?
解决方案 »
- 请问如何用jquery识别图片并点击弹出层放大(一张图片非缩略和原图关系)
- 关于JS意外的调用了方法或属性访问
- 请问怎样使框的右边不可以拉伸,而其它三边可以拉伸?
- 如何替换正斜杠?
- window.open问题
- Extjs combobox valueNotFoundText不生效
- select控件的当前选定项的文本属性(Text)是哪个属性啊?好像select控件只有value属性,没有Text属性
- 怎样用正则表达式(javascript)验证输入的是正整数?而没有英文字符?
- js存入读取txt文件实现登录注册
- 高手帮我:我如何判断一个中文字符串的长度,内容如下:
- 关于js跨域的问题,在线急等
- 找到一个免费jquery代码调试工具,适合初学者用
var p = /^www\.[a-zA-Z0-9]+\.(com|cn)$/g;
var str="www.baidu.com";
alert(str.match(p));
p = /^(ht|f)tp:\/\/www\.[a-zA-Z0-9]+\.(com|cn)$/g;
str="http://www.baidu.com";
alert(str.match(p));</script>
匹配 . 在正则内需要转义 function _tests()
{
var txts=document .getElementById("txt").value ;
var context=(/^((?:ht|f)tp:\/\/)?www\.[a-zA-Z0-9]+\.(?:com|cn)$/gi);
while (res=context.exec(txts))
{
window .alert (res[0]);
}
}
要是再在下面写一行就检测不出来?有电子邮件的正则表达式吗?
<html>
<head>
<meta charset="UTF-8">
<title>test</title>
<script src="jquery.js"></script>
</head>
<body>
<textarea id="txt"rows="10" cols="20"></textarea>
<input type ="button" value ="测试" onclick ="_tests()"/>
<script type="text/javascript">
function _tests(){
var res,
txts = document.getElementById("txt").value,
context = (/^(?:http:|ftp:)?w{3}\.[a-zA-Z0-9]+\.(?:com|cn)$/);
alert(context.exec(txts));
}
</script>
</script>
</body>
</html>
<html>
<head>
<meta charset="UTF-8">
<title>test</title>
<script src="jquery.js"></script>
</head>
<body>
<textarea id="txt"rows="10" cols="20"></textarea>
<input type ="button" value ="测试" onclick ="_tests()"/>
<script type="text/javascript">
function _tests(){
var res,
txts = document.getElementById("txt").value,
context = (/^(?:http:|ftp:)?w{3}\.[a-zA-Z0-9]+\.(?:com|cn)$/gm);
while(res = context.exec(txts)){
window.alert(res[0]);
}
}
</script>
</script>
</body>
</html>
res是exec查找的结果,为一个数组,取第一个元素(全匹配)打印出来。。