删除“http://"和“https://"从一个字符串 [英] Remove "http://" and "https://" from a string

查看:86
本文介绍了删除“http://"和“https://"从一个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 ruby​​ 新手使用正则表达式.如何从字符串中删除 httpshttpwww

am new to ruby using regular expression .how can i remove https and http and www from a string

server= http://france24.miles.com
server= https://seloger.com

我想从这些网站中删除所有 http、https 和 www

from these sites i want to remove all http ,https and www

france24.miles.com
seloger.com

我使用了以下代码,但它不适合我

i used following code but it is not woking for me

server = server.(/^https?\:\/\/(www.)?/,'')

推荐答案

server = server.(/^https?\:\/\/(www.)?/,'')

这不起作用,因为您没有调用字符串 server 的方法.确保你调用了 sub 方法:

This didn't work, because you aren't calling a method of the string server. Make sure you call the sub method:

server = server.sub(/^https?\:\/\/(www.)?/,'')

示例

> server = "http://www.stackoverflow.com"
> server = server.sub(/^https?\:\/\/(www.)?/,'')
stackoverflow.com

根据要求,如果您希望它也使用非法格式 http:\\,请使用以下正则表达式:

As per the requirement if you want it to work with the illegal format http:\\ as well, use the following regex:

server.sub(/https?\:(\\\\|\/\/)(www.)?/,'')

这篇关于删除“http://"和“https://"从一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆