且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

删除“www"、“http://"从字符串

更新时间:2023-02-23 09:29:07

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

如果你不想使用 s =,你应该使用 sub!s 而不是所有的 subs.

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

您的代码存在的问题是:

The problems with your code are:

  1. 问号总是跟在一个可选字符之后
  2. 始终替换子程序中的一种模式.您可以链接"多个操作.
  3. 在 Regexp 的开头使用 sub 而不是 gsub^ 所以它只替换 http://代码> 在开头,但将代码留在中间.
  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.