Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9178425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:32:54+00:00 2026-06-17T17:32:54+00:00

How do I do a replace that will turn all plain addresses in a

  • 0

How do I do a replace that will turn all plain addresses in a paragraph in to links?

The problem is periods are valid in the address, but the address may be at the end of a sentence.

Test string:

The link is: http://www.google.com/pants.  And that is the link.  

I need to group out from http to pants. (It is plain text and I need to make it html.)

This regex grabs the period after pants and so is bad:

(^|[\n ])([\w]+?://[^\s]*)

I’m pretty sure I need to do a lookahead, but I can’t put that in the ^\s character set. Trying to do an ifthenelse has also eluded me.

Here is my output thingy:

$1<a href=\"$2\" target=\"_blank\">$2</a>

Hey, people reading this. Make sure you know this site: http://gskinner.com/RegExr/ It rules. It’s the only reason I get any regex right.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T17:32:56+00:00Added an answer on June 17, 2026 at 5:32 pm

    Assuming no space in the urls and a space or end of string after them:

    str = str.replace(
        /(https?:\/\/\S+?)(?=\.?(\s|$))/g,
            '<a href="$1" target="_blank">$1</a>' );
    

    It captures ‘http[s]://’ and non-space characters as few times as possible until looking ahead there is optionally . and then a space or the end of string.

    If you want to exclude other punctuation that may be at the end of an url you could change the positve lookahead accordingly, e.g. (?=[;:!,.]?(?:\s|$)).

    Note that the above regex is not intended to only match valid urls, and you may want to replace the \S with [\w/.-] to only match urls containing word characters and .-/.

    In search of the perfect URL validation regex

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am looking for a single regular expression that will replace all replace me
I would like to create a regular expression that will replace a normal space
How do you do a query-replace-regexp in Emacs that will match across multiple lines?
This works: var.replace(/[^0-9]+/g, ''); That simple snippet will replace anything that is not a
I need to write a regex, that will traverse through my file and replace
The function signature on PHP.net for array_replace() says that the arrays will be passed
I have a select query. I want to replace that select query with another
I have a large (600 odd) set of search and replace terms that I
I search the web alot and didn't find c++ function that replace xml Special
I search the web alot and didn't find js function that replace xml Special

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.