2011-04-07 69 views

回答

0

这个怎么样?

String.prototype.startsWith = function(str) 
{return (this.match("^"+str)==str)} 

if($("#textboxID").val().startsWith("http://")) 
    alert('contains http://'); 
+0

正则表达式的特殊字符应该可以在'startsWith'中转义。无论是该函数还是该函数都可以用'matchesPrefix'来命名,以提供一个线索,该参数实际上是一个正则表达式,其中前缀“^”。 – 2011-04-07 22:39:03

0

使用正则表达式:

if($("#my-input").val().match(/^(?:http:\/\/|www)/)) { 
    // starts with http:// or www 
} 
1

您可以检查此使用正则表达式。下面经过的所有文本框和打印,如果他们以http安慰://或www或不

$('input[type=text]').each(function() { 
    console.log($(this).val().match(/(^http:\/\/)|(^www)/) != null); 
}) 

fiddle http://jsfiddle.net/ZQRg2/1/

3

不需要昂贵的正则表达式。

String.prototype.startsWith = function(str) { 
    return (this.length >= str.length) 
     && (this.substr(0, str.length) == str); 
} 

String.prototype.startsWith_nc = function(str) { 
    return this.toLowerCase().startsWith(str.toLowerCase()); 
} 

var text = $('#textboxID').val(); 
if (text.startsWith_nc("http://") || text.startsWith_nc("www")) { 
    alert("looks like a URL"); 
} 
0

我同意Tomalak,这里不需要昂贵的正则表达式。

if($("#textboxID").val().indexOf("http://") == 0) 
    alert('contains http://'); 
相关问题