我试图设置一个regexp,它将检查字符串的开始,如果它包含http://或https://,它应该匹配它。
我该怎么做呢?我正在尝试下面的,但它不起作用:
^[(http)(https)]://
我试图设置一个regexp,它将检查字符串的开始,如果它包含http://或https://,它应该匹配它。
我该怎么做呢?我正在尝试下面的,但它不起作用:
^[(http)(https)]://
当前回答
不分大小写:
var re = new RegExp("^(http|https)://", "i");
var str = "My String";
var match = re.test(str);
其他回答
使这种大小写不敏感在asp.net中不起作用,所以我只指定了每个字母。
下面是我要做的让它在asp.net RegularExpressionValidator中工作:
[Hh][Tt][Tt][Pp][Ss]?://(.*)
注:
(?i)和使用/whatever/i不能工作,可能是因为javascript没有带来所有区分大小写的功能 最初在开始时有^,但没关系,但(.*)有(表达式没有(.*)就不能工作,但没有^就可以工作) 不需要转义//虽然可能是个好主意。
如果你需要的话,这里是完整的RegularExpressionValidator:
<asp:RegularExpressionValidator ID="revURLHeaderEdit" runat="server"
ControlToValidate="txtURLHeaderEdit"
ValidationExpression="[Hh][Tt][Tt][Pp][Ss]?://(.*)"
ErrorMessage="URL should begin with http:// or https://" >
</asp:RegularExpressionValidator>
这也适用于URL编码的字符串。
^(https?)(:\/\/|(\%3A%2F%2F))
^https?:\/\/(.*)其中(.*)匹配https://之后的所有内容
(http | https) ?: \ \ / (\ S +)
这对我很有用
不是一个正则表达式专家,但我会尝试解释awnser。
(http|https):圆括号表示捕获组,“I”表示OR语句。
\/\/: "\"允许使用特殊字符,例如"/"
(\S+):任何在下一次空白之前都不是空白的内容
不分大小写:
var re = new RegExp("^(http|https)://", "i");
var str = "My String";
var match = re.test(str);