SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Evangelist NokX's Avatar
    Join Date
    Feb 2003
    Location
    Knoxville, TN
    Posts
    525
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Finding a url in a string and turning it into a link

    i have a string that can have one or more urls within the string. i'm wanting to use javascript to find the urls and turn them into links.

    i know you'd do a search with a regular expression, but i can't find the correct one on the web...even though i know there has to be a billion examples out there. my google search skills are s#&@ today.

    before: "some text telling me to visit http://www.something.com and http://more.com"

    after: "some text telling me to visit http://www.something.com and http://more.com"

  2. #2
    SitePoint Evangelist NokX's Avatar
    Join Date
    Feb 2003
    Location
    Knoxville, TN
    Posts
    525
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well... i didn't tell the first url to be a link, but i'm guessing sitepoint is doing something similar to what i wanna do; except i want urls without a "www" to work, as well.

  3. #3
    SitePoint Zealot
    Join Date
    May 2009
    Location
    Netherlands
    Posts
    189
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    str.replace(/(https?:\/\/[\w\.]+[a-z]{2,})\b/gi, '<a href="$1">$1</a>')


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •