Samuli Hakoniemi
Samuli Hakoniemi

Reputation: 19049

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?

Upvotes: 0

Views: 59

Answers (2)

sinisake
sinisake

Reputation: 11328

input = 'something();_c.log(<ANY CONTENT HERE>);somethingElse();'
input = input.replace(/_c.log\(.*?\);/g, "");

http://jsfiddle.net/2SY3w/

Upvotes: 0

Sabuj Hassan
Sabuj Hassan

Reputation: 39365

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).

If the something and somethingElse are unknown, and the _c.log is fixed over there, then use this one:

input = input.replace(/(\w+\(\);)_c\.log.*?(\w+\(\);)/g, "$1$2");

Upvotes: 2

Related Questions