我正在尝试设置一个regexp来检查一个字符串的开头,如果它包含一个http://
或者https://
它应该匹配它.
我怎样才能做到这一点?我正在尝试以下无效的方法:
^[(http)(https)]://
Run Code Online (Sandbox Code Playgroud)
cdh*_*wie 328
你的使用[]
是不正确的 - 注意[]
表示一个字符类,因此只匹配一个字符.该表达式[(http)(https)]
转换为"匹配a (
,an h
,a t
,a t
,a p
,a )
或an" s
.(忽略重复的字符.)
试试这个:
^https?://
Run Code Online (Sandbox Code Playgroud)
如果您确实想要使用替换,请改用以下语法:
^(http|https)://
Run Code Online (Sandbox Code Playgroud)
mis*_*hap 38
不区分大小写:
var re = new RegExp("^(http|https)://", "i");
var str = "My String";
var match = re.test(str);
Run Code Online (Sandbox Code Playgroud)