Verify if some string is valid URL, JavaScript

PHOTO EMBED

Sat Jan 08 2022 17:15:53 GMT+0000 (Coordinated Universal Time)

Saved by @juliyvchirkov #javascript #url #validation #string

/**
 * Provides quick & simple way to verify if some
 * string is valid URL
 *
 * @param   <string>  URL to verify
 * @returns <boolean> Returns true if passed string
 *                    is valid URL, false otherwise
 */
function validateUrl (url) {
    try {
        new URL(url)
    } catch (error) {
        return false
    }
  
    return true
}
content_copyCOPY