JS Regex url validation

后端 未结 4 537
梦毁少年i
梦毁少年i 2021-02-07 12:41

I tried to validate url with or without http No matter what i did the function return false. I checked my regex string in this site: http://regexr.com/ And its seen as i expect.

4条回答
  •  难免孤独
    2021-02-07 13:11

    Actually, this question needs a powerful regex and the following code is not very hard to understand, please see below(ES6):

    const isValidUrl = urlString => {
      const urlRegex = /^((http(s?)?):\/\/)?([wW]{3}\.)?[a-zA-Z0-9\-.]+\.[a-zA-Z]{2,}(\.[a-zA-Z]{2,})?$/g;
      const result = urlString.match(urlRegex);
      return result !== null;
    };
    

提交回复
热议问题