[英]Javascript regex to match all characters to certain point inside content
I'm having following kind of content in my code: 我在我的代码中有以下内容:
something();_c.log(<ANY CONTENT HERE>);somethingElse();
Now I'd like to have a regexp that returns: 现在我想要一个返回的正则表达式:
something();somethingElse();
For some reason I can't get this (easily) done. 出于某种原因,我无法完成(轻松)。 How can I achieve the desired result? 我怎样才能达到预期的效果?
Can you use string replace with regex?? 你可以用正则表达式替换字符串??
input = 'something();_c.log(<ANY CONTENT HERE>);somethingElse();'
input = input.replace(/(something\(\);).*?(somethingElse\(\);)/g, "$1$2");
Here it is capturing the two groups from your input and replacing everything excepts the two groups( $1
, $2
). 在这里,它从您的输入中捕获两个组并替换除两个组之外的所有组( $1
, $2
)。
If the something and somethingElse are unknown, and the _c.log is fixed over there, then use this one: 如果something和somethingElse是未知的,并且_c.log在那里被修复,那么使用这个:
input = input.replace(/(\w+\(\);)_c\.log.*?(\w+\(\);)/g, "$1$2");
input = 'something();_c.log(<ANY CONTENT HERE>);somethingElse();'
input = input.replace(/_c.log\(.*?\);/g, "");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.