Check if a string starts with http using Javascript

前端 未结 6 1853
闹比i
闹比i 2021-02-11 13:56

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:36

    Non-Regex declarative way:

    export const hasValidUrlProtocol = (url = '') => 
        Boolean(['http://', 'https://', 'ftp://'].some(protocol => url.startsWith(protocol)))
    

提交回复
热议问题