如何替换字符串中除第一次出现之外的所有模式
快速问题:我的模式是一个 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以尝试否定前瞻,避免字符串的开头:
查看是否在线: jsfiddle
You can try a negative lookahead, avoiding the start of the string:
See if online: jsfiddle
没有 JS RegExp 可以替代除第一个模式匹配之外的所有内容。但是,您可以通过将函数作为第二个参数传递给
replace
方法来实现此行为。每次匹配都会执行函数 (
fn_replaceBy
)。在第一次匹配时,函数立即返回匹配的字符串(什么也没发生),并设置一个标志。其他所有匹配项将根据函数中描述的逻辑进行替换:通常,您使用
$0 $1 $2
等来引用组。在fn_replaceBy
中,函数参数等于:第一个参数 =$0
,第二个参数 =$1
,等等。匹配的子字符串将被函数
fn_replaceBy
的返回值替换。使用函数作为替换的第二个参数可以实现非常强大的应用程序,例如智能 HTML 解析器。另请参阅:MDN:String.replace >指定函数作为参数
There's no JS RegExp to replace everything-but-the-first-pattern-match. You can, however, implement this behaviour by passing a function as a second argument to the
replace
method.The function (
fn_replaceBy
) is executed for each match. At the first match, the function immediately returns with the matched string (nothing happens), and a flag is set.Every other match will be replaced according to the logic as described in the function: Normally, you use
$0 $1 $2
, et cetera, to refer back to groups. Infn_replaceBy
, the function arguments equal these: First argument =$0
, second argument =$1
, et cetera.The matched substring will be replaced by the return value of function
fn_replaceBy
. Using a function as a second parameter forreplace
allows very powerful applcations, such as an intelligent HTML parser.See also: MDN: String.replace > Specifying a function as a parameter
这不是最漂亮的解决方案,但您可以用任意的东西(如占位符)和链替换来替换第一次出现,以实现其余的逻辑:
生成:
-983247924234.01
这只是扩展了已接受的答案对于任何寻找不能依赖于第一个匹配/出现是字符串中的第一个字符的示例的人。
It's not the prettiest solution, but you could replace the first occurrence with something arbitrary (like a placeholder) and chain replacements to fulfill the rest of the logic:
Produces:
-983247924234.01
This merely expands on the accepted answer for anyone looking for an example that can't depend on the first match/occurrence being the first character in the string.
确保第一个
'l'
前面没有空格,并删除'l'
后面的任何空格。makes sure the first
'l'
is not preceded by space and removes any space followed by an'l'
.我在 https://www.regextester.com/99881 找到了这个解决方案,使用了lookbehind模式:
/(?<=(.*l.*))l/g
或者更一般地说
/(?<=(.*MYSTRING.*))MYSTRING/g
其中
MYSTRING
是您要删除的内容。(顺便说一句,这也可能是一个有用的字符串,用于删除电子邮件主题字符串中除第一次出现的“Re:”之外的所有内容。)
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.)
像这样的东西吗?
"l 5 0 l 0 10 l -5 0 l 0 -10".replace(/[^^]l/g, '')
Something like this?
"l 5 0 l 0 10 l -5 0 l 0 -10".replace(/[^^]l/g, '')