Check if a string starts with http using Javascript

前端 未结 6 1363
不思量自难忘°
不思量自难忘° 2021-02-11 14:27

I\'ve been searching all over for an answer to this and all of the answers I\'ve found haven\'t been in JavaScript.

I need a way, in javascript, to check if a string sta

6条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-11 14:46

    This should work:

    var pattern = /^((http|https|ftp):\/\/)/;
    
    if(!pattern.test(url)) {
        url = "http://" + url;
    }
    

    jsFiddle

提交回复
热议问题