在正则表达式中使用否定条件 [英] using negative conditions within regular expressions
本文介绍了在正则表达式中使用否定条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以在gsub表达式中使用否定匹配?
我想替换以hello
开头的字符串,以hello Peter
Is it possible to use negative matches within gsub expressions?
I want to replace strings starting by hello
except those starting by hello Peter
my-string.gsub(/^hello@/i, '')
我应该代替@
放什么?
推荐答案
听起来像是您想要否定的前瞻:
Sounds like you want a negative lookahead:
>> "hello foo".gsub(/hello (?!peter)/, 'lala ') #=> "lala foo"
>> "hello peter".gsub(/hello (?!peter)/, 'lala ') #=> "hello peter"
这篇关于在正则表达式中使用否定条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文