Regular expression for URL

后端 未结 10 2016
刺人心
刺人心 2020-12-03 01:04

I have written regex to validate URL which could be either like

  • example.com

  • www.example.com

  • http://www.example.com

10条回答
  •  离开以前
    2020-12-03 01:39

    This is to support both http, https as optional and validate blank space is not included in url.

    this.isValidURL = function (url) {
    
        if (!url) return false;
    
        const expression = /^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$/gm;
    
        return url.match(new RegExp(expression));
    }
    

提交回复
热议问题