所以我有这个代码:
function validateText(str)
{
var tarea = str;
var tarea_regex = /^(http|https)/;
if(tarea_regex.test(String(tarea).toLowerCase()) == true)
{
$('#textVal').val('');
}
}
Run Code Online (Sandbox Code Playgroud)
这适用于此:
https://hello.com
http://hello.com
但不适用于:
这是一个网站http://hello.com asdasd asdasdas
尝试做一些阅读但我不在哪里放*?因为他们会根据这里检查字符串上任何地方的表达式 - > http://www.regular-expressions.info/reference.html
谢谢
D. *_*out 12
从它的外观来看,你只是检查字符串中是否存在http或https.正则表达式对于此目的来说有点过分.尝试使用以下简单代码indexOf:
function validateText(str)
{
var tarea = str;
if (tarea.indexOf("http://") == 0 || tarea.indexOf("https://") == 0) {
// do something here
}
}
Run Code Online (Sandbox Code Playgroud)
试试这个:
function validateText(string) {
if(/(http(s?)):\/\//gi.test(string)) {
// do something here
}
}
Run Code Online (Sandbox Code Playgroud)
小智 5
^开头的 与字符串的开头匹配。只需将其删除即可。
var tarea_regex = /^(http|https)/;
Run Code Online (Sandbox Code Playgroud)
应该
var tarea_regex = /(http|https)/;
Run Code Online (Sandbox Code Playgroud)