如何替换所有BUT字符串中第一次出现的模式 [英] How to replace all BUT the first occurrence of a pattern in string
本文介绍了如何替换所有BUT字符串中第一次出现的模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
快速问题:我的模式是一个svg字符串,它看起来像 l 5 0 l 0 10 l -5 0 l 0 -10
进行一些单元测试比较参考我需要抛弃除了第一个 l
之外我知道我可以抛弃它们并预先设置'l',或者我可以使用子串。但是我想知道这有一个javascript正则表达式成语吗?
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 but the first l
I know i can ditch them all and put an 'l' upfront, or I can use substrings. But I'm wondering is there a javascript regexp idiom for this?
推荐答案
你可以尝试一个负向前瞻,避免开始字符串:
You can try a negative lookahead, avoiding the start of the string:
/(?!^)l/g
查看是否在线: jsfiddle
这篇关于如何替换所有BUT字符串中第一次出现的模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文