Regex to test if string begins with http:// or https://

后端 未结 9 953
没有蜡笔的小新
没有蜡笔的小新 2020-12-04 06:41

I\'m trying to set a regexp which will check the start of a string, and if it contains either http:// or https:// it should match it.

How c

9条回答
  •  清歌不尽
    2020-12-04 07:19

    ^ for start of the string pattern,

    ? for allowing 0 or 1 time repeat. ie., s? s can exist 1 time or no need to exist at all.

    / is a special character in regex so it needs to be escaped by a backslash \/

    /^https?:\/\//.test('https://www.bbc.co.uk/sport/cricket'); // true
    
    /^https?:\/\//.test('http://www.bbc.co.uk/sport/cricket'); // true
    
    /^https?:\/\//.test('ftp://www.bbc.co.uk/sport/cricket'); // false
    

提交回复
热议问题