Check if a JavaScript string is a URL

前端 未结 30 3580
野趣味
野趣味 2020-11-22 15:41

Is there a way in JavaScript to check if a string is a URL?

RegExes are excluded because the URL is most likely written like stackoverflow; that is to s

30条回答
  •  我在风中等你
    2020-11-22 16:24

    In my case my only requirement is that the user input won't be interpreted as a relative link when placed in the href of an a tag and the answers here were either a bit OTT for that or allowed URLs not meeting my requirements, so this is what I'm going with:

    ^https?://.+$
    

    The same thing could be achieved pretty easily without regex.

提交回复
热议问题