Skip to content
The Chat Archive
gpt-3.5-turbo-0301· OpenAI WildChat-4.8M (AI2)

Write regexp in JS to search URL exluding href attribute

  1. User

    Write regexp in JS to search URL exluding href attribute

  2. Assistant

    To search for a URL pattern excluding the href attribute, you can use the following regular expression in JavaScript:

    /(http(s)?:\/\/)?(www\.)?[a-zA-Z0-9]+\.[a-zA-Z]+(\/[a-zA-Z0-9#]+)*\/?[^>]*?(?=[^>]*?(<|$))/ig
    

    This regular expression will match any URLs in the given input string except those that appear within an href attribute.

    Here is an explanation of the pattern:

    • (http(s)?:\/\/)?: matches the optional http:// or https:// at the beginning of the URL
    • (www\.)?: matches the optional www. in the domain
    • [a-zA-Z0-9]+\.[a-zA-Z]+: matches the domain name (e.g. google.com)
    • (\/[a-zA-Z0-9#]+)*\/?: matches any additional path segments in the URL, including any hash fragments (#)
    • [^>]*?: matches any characters that are not closing HTML tags (>)
    • (?=[^>]*?(<|$)): positive lookahead to ensure the URL is not followed by > (closing tag) or end-of-line characters

    The ig flags at the end of the expression make it case-insensitive and global, respectively.