How to replace all BUT the first occurrence of a pattern in string

后端 未结 6 735
余生分开走
余生分开走 2020-12-03 07:04

quick question: my pattern is an svg string and it looks like l 5 0 l 0 10 l -5 0 l 0 -10 To do some unittest comparison against a reference I need to ditch all

6条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-03 07:31

    I found this solution at https://www.regextester.com/99881, using a lookbehind pattern:

    /(?<=(.*l.*))l/g

    Or more generally

    /(?<=(.*MYSTRING.*))MYSTRING/g

    where MYSTRING is something that you want to remove.

    (This may also be a useful string for removing all but the first occurrence of "Re:" in an email subject string, by the way.)

提交回复
热议问题