2014-09-08 1 views
0
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"); 
} 

Mon code fonctionne jusqu'à ce que je tente de « tweet » un site Web. si je tape www ou .com comme un tweet, il me dira qu'il a détecté une URL qui est bonne. MAIS quand je tape xxxxxx www xxxxxx (x étant n'importe quoi) il ne détectera pas l'URL.Ma recherche « fonction » ne fonctionne pas

+0

Je n'ai pas lu même votre question, et je ne sera probablement pas à moins que vous reformater votre code et le rendre plus lisible. Utilisez l'indentation appropriée s'il vous plaît. – EmmaGamma

+0

Je changerais un peu la logique. CHeck ce violon: http://jsfiddle.net/8s7db3x5/1 - fondamentalement vérifier les mots-clés _before_ confirmant le message de succès - aussi, supprimez l'espace avant et après 'www' et' http' – karthikr

+2

Je recommande également de lire le [ ** documentation ** de 'search'] (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/search). Par exemple. il ne prend qu'un argument, qui est censé être une expression régulière. Les autres que vous passez ("http" et ".com") sont simplement ignorés. –

Répondre

0

Vous pouvez faire une recherche plus précise en utilisant jQuery. La fonction '.inArray()' devrait faire l'affaire.

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> 
0

Vous pouvez essayer d'utiliser des expressions régulières

var cat = prompt("What Do You Want To Tweet??!"), 
    expression = /[[email protected]:%_\+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[[email protected]:%_\+.~#?&//=]*)?/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"); 
} 
Questions connexes