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 7818235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T06:30:52+00:00 2026-06-02T06:30:52+00:00

How can I remove www, http://, https:// from strings using Ruby? I tried this

  • 0

How can I remove “www”, “http://”, “https://” from strings using Ruby?

I tried this but it didn’t work:

s.gsub('/(?:http?:\/\/)?(?:www\.)?(.*)\/?$/i', '')

Here what I’m doing in Rails:

<%= auto_link(job.description) do |url| url.truncate(25).gsub('http://', '') end %>

Url are truncated, but my goal is to remove the beginning of the links, such as “www” or “http://&#8221; so the link would look like “google.com/somepage/d…”, not like “http://google.com/some…&#8221;

  • 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-02T06:30:55+00:00Added an answer on June 2, 2026 at 6:30 am
    s = s.sub(/^https?\:\/\//, '').sub(/^www./,'')
    

    If you don’t want to use s =, you should use sub!s instead of all subs.

    The problems with your code are:

    1. Question mark always follows AFTER an optional character
    2. Always replace one pattern in a sub. You can “chain up” multiple operations.
    3. Use sub instead of gsub and ^ in the beginning of Regexp so it only replaces the http:// in the beginning but leaves the ones in the middle.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was removed icon applicaion like this -> http://www.helloandroid.com/tutorials/removing-app-icon-launcher But I can't launch it
How would I remove a view from this dialog? I know I can remove
As I can see here http://www.sqlstrings.com/MS-Access-connection-strings.htm one could Open connection to Access database located
I have been trying to scrape the 'School Notices' from this URL http://www.isleworthsyon.hounslow.sch.uk/ I
I've looked through all the stuff to remove characters from strings but I don't
I am using this guide: http://www.asp.net/mvc/tutorials/mvc-music-store-part-7 Like instructed, I entered to ASP.NET Web Application
One can remove all calls to printf() using #define printf . What if I
How to I can remove posts from a specific category from homepages in wordpress?
I was wondering if when using the datagridview control you can remove the thing
I want to know is there any way that I can remove duplicates from

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.