我的搜索“功能”不起作用
My search "function" does not work
var cat = prompt("What Do You Want To Tweet??!");
if(cat.length >= 140)
{
confirm("Sorry but your Tweet was over the 140 character limit by: " + (cat.length - 140));
confirm("You need to resubmit a Tweet that is not over the 140 Character limit");
}
else{
confirm("Success, Your Tweet has been posted!");
}
//scan for websites
if (cat.search(" www ", " http ", ".com"))
{
}
else{
confirm("We have Detected a URL in your Tweet");
}
我的代码一直工作到我尝试"推特"一个网站。 如果我输入www或.com作为推文,它会告诉我它检测到了一个好的URL。但是当我输入 xxxxxx www xxxxxx(x 是其他任何东西(时,它不会检测到 URL。
你可以使用 jQuery 进行更准确的搜索。'.inArray((' 函数应该可以解决问题。
http://api.jquery.com/jquery.inarray/
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery.inArray demo</title>
<style>
div {
color: blue;
}
span {
color: red;
}
</style>
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<div>"John" found at <span></span></div>
<div>4 found at <span></span></div>
<div>"Karl" not found, so <span></span></div>
<div>"Pete" is in the array, but not at or after index 2, so <span></span></div>
<script>
var arr = [ 4, "Pete", 8, "John" ];
var $spans = $( "span" );
$spans.eq( 0 ).text( jQuery.inArray( "John", arr ) );
$spans.eq( 1 ).text( jQuery.inArray( 4, arr ) );
$spans.eq( 2 ).text( jQuery.inArray( "Karl", arr ) );
$spans.eq( 3 ).text( jQuery.inArray( "Pete", arr, 2 ) );
</script>
</body>
</html>
您可以尝试使用正则表达式
var cat = prompt("What Do You Want To Tweet??!"),
expression = /[-a-zA-Z0-9@:%_'+.~#?&//=]{2,256}'.[a-z]{2,4}'b('/[-a-zA-Z0-9@:%_'+.~#?&//=]*)?/gi,
regex = new RegExp(expression);
if(cat.length >= 140) {
confirm("Sorry but your Tweet was over the 140 character limit by: " + (cat.length - 140));
confirm("You need to resubmit a Tweet that is not over the 140 Character limit");
}
else{
confirm("Success, Your Tweet has been posted!");
}
//scan for websites
if (cat.match(regex)) {
confirm("We have dectected a URL in your Tweet");
}
else{
confirm("no URL");
}
相关文章:
- 使用复杂数组时Jquery自动完成功能不起作用
- 为什么我的流星每个功能都不起作用
- jQuery点击功能在获取请求后不起作用
- 切换功能不起作用
- 为什么不是'我的修剪功能不起作用
- Angular UI Select2指令搜索功能不起作用
- j查询点击功能在手风琴菜单链接上不起作用
- 为什么这个排序功能在 Safari 中不起作用
- 控制器功能在 Angular.js 中不起作用
- AngularJS 路由功能不起作用
- JavaScript更新功能不起作用
- ScriptX 打印功能在 IE8 中不起作用
- JS表单验证功能不起作用
- 本地存储功能中的图像不起作用
- 复选框树视图功能不起作用
- Web音频-AudioParam调度功能不起作用
- 功能的禁用/启用按钮;不起作用
- JavaScript/jQuery:获取选择功能在Firefox和Chrome中不起作用
- 复选框单击功能不起作用
- Ckeditor插件功能在使用setData(“hai”)之后不起作用;