首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么这不管用?不过,它适用于Rubular。

为什么这不管用?不过,它适用于Rubular。
EN

Stack Overflow用户
提问于 2014-05-06 01:13:51
回答 1查看 61关注 0票数 1
代码语言:javascript
复制
text.gsub!(/(?<!http:\/\/)www\.(?=\w)/,'http://www.')

我have...and说的是未定义的(?)序列。当我在rubular中使用(?<!http:\/\/)www\.(?=\w)时,它做了我想做的事情,但是这对我来说不起作用,所以我需要一些帮助。

这应该是用来代替www. to http://www.的,这是一堂课的作业,所以它必须做到这一点。

非常感谢你的帮助!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-05-06 01:22:47

试试这个:

代码语言:javascript
复制
text.gsub(/^(?!http:\/\/)www(.*?)$/, 'http://www\1')

Regex演示

它将www的所有实例(而不是http://前面的实例)替换为http://www.something.com

Regex解释

代码语言:javascript
复制
Assert position at the beginning of a line (at beginning of the string or after a line break character) «^»
Assert that it is impossible to match the regex below starting at this position (negative lookahead) «(?!http://)»
   Match the characters “http://” literally «http://»
Match the characters “www” literally «www»
Match the regular expression below and capture its match into backreference number 1 «(.*?)»
   Match any single character that is not a line break character «.*?»
      Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
Assert position at the end of a line (at the end of the string or before a line break character) «$»
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23484446

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档