我的搜索“功能"不起作用 [英] My search "function" does not work

查看:36
本文介绍了我的搜索“功能"不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 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.


My code works up until I try to "tweet" a website. if I type www or .com as a tweet, it will tell me it has detected a url which is good. BUT when I type xxxxxx www xxxxxx (x being anythingelse) it wont detect the url.

推荐答案

可以尝试使用正则表达式

You can try using regular expressions

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");   
}

这篇关于我的搜索“功能"不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆