Javascript regex to find a base URL

前端 未结 2 973
粉色の甜心
粉色の甜心 2021-01-25 17:34

I\'m going mad with this regex in JS:

var patt1=/^http(s)?:\\/\\/[a-z0-9-]+(.[a-z0-9-]+)*?(:[0-9]+)?(\\/)?$/i;

If I give an input string like \

2条回答
  •  粉色の甜心
    2021-01-25 17:56

    I'm no javascript pro, but accustomed to perl regexp, so I'll give it a try; the . in the middle of the regexp might need to be escaped, as it can map a / and jinx the whole regexp.

    Try this way:

    var patt1=/^http(s)?:\/\/[a-z0-9-]+(\.[a-z0-9-]+)*?(:[0-9]+)?(\/)?$/i; 
    

提交回复
热议问题