What is a regex to match a string NOT at the end of a line?

后端 未结 1 1311
梦毁少年i
梦毁少年i 2020-12-13 12:14

The regex /abc$/ will match an abc that does appear at the end of the line. How do I do the inverse?

I want to match abc that

相关标签:
1条回答
  • 2020-12-13 13:02
    /abc(?!$)/
    

    (?!$) is a negative lookahead. It will look for any match of abc that is not directly followed by a $ (end of line)

    Tested against

    • abcddee (match)
    • ffffdeeeabc (no match)
    • adfassdfabcs (match)
    • fabcddee (match)

    applying it to your case:

    ruby-1.9.2-p290 :007 > "aslkdjfabcalskdfjaabcaabc".gsub(/abc(?!$)/, 'xyz')
      => "aslkdjfxyzalskdfjaxyzaabc" 
    
    0 讨论(0)
提交回复
热议问题