1

Is there an easy way to strip out all instances of:

console.log("items");

Where items is a string that's always different (different length, different text)?

redconservatory
  • 21,438
  • 40
  • 120
  • 189

3 Answers3

5

Here's the regex for that:

console\.log\("(\\"|[^"])*"\);

See it here in action: http://regexr.com?2vmmk


As pointed out by @Benoit, the following solution is superior to the one above:

console\.log\("([^\\"]|\\.)*"\);

See it here in action: http://gskinner.com/RegExr/?2vmmt


You could probably catch many more scenarios with this:

console\.log\(.*\);

if you're not in multiline mode, and you're sure your statements are on their own lines (meaning no other statement will be on that same line).

If they're not on their own lines, you might try this:

console\.log\([^;]\);

but this'll not match this piece:

console.log( (function(){ return 3*4; })() );

or even this:

console.log(";");

since they have a ; in the middle.


In conclusion: it all depends on your needs, which haven't been clearly specified.

Joseph Silber
  • 214,931
  • 59
  • 362
  • 292
  • `console.log("Hello \"world\"");` – Lightness Races in Orbit Jan 11 '12 at 16:07
  • `console.log("Hello " + "world");` – Linus Kleen Jan 11 '12 at 16:10
  • @LinusKleen - The OP said there will be a string in there. If you go down that path, you'll never get out. What would you say about: `console.log("Hello " + myArray[23]);` or `console.log("Hello " + myObject["key"]);` or even `console.log("Hello " + myFunc(myObject["key"]));` for that matter. If you want to parse Javascript in regex, you'll have a **really** hard time. The OP clearly asked to match a string. – Joseph Silber Jan 11 '12 at 16:12
  • That's why regular expressions are _not_ the correct solution to this problem. – Lightness Races in Orbit Jan 11 '12 at 16:13
  • @Benoit - I don't get your point. The regex will match the statement, and ignore the comment: http://regexr.com?2vmm2 – Joseph Silber Jan 11 '12 at 16:26
  • @Joseph Silber: Actually my example (comment removed) was bad: `console.log("\");");`. The problem with your regex is that the backslash will already be eaten because it matches `[^"]`, so your match will end at the first `"` no matter whether it has been escaped. http://gskinner.com/RegExr/?2vmme – Benoit Jan 11 '12 at 16:35
  • @Benoit - Well that's a good point, and I've updated the regex above. See it [here in action](http://regexr.com?2vmmh). Thanks. – Joseph Silber Jan 11 '12 at 16:39
  • @Benoit - I don't think the OP minds losing his comments. If he does, I can't really help him with a regex... – Joseph Silber Jan 11 '12 at 16:43
  • @Joseph Silber: Imagine there is code before the comment... Or code with a string literal beginning with `);`? Okay this is being very pecky. But you will have realized something at least :) – Benoit Jan 11 '12 at 16:43
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/6624/discussion-between-benoit-and-joseph-silber) – Benoit Jan 11 '12 at 16:44
  • @LightnessRacesinOrbit: If not RegEx what else? Please give us some insight. BTW.: RegEx are used by Linguists as an exact reprensentation of an arbitrary statement. – Nikodemus RIP Jan 11 '12 at 16:47
  • @Nikodemus: No, there are loads of things that regular expressions cannot parse in totality. For example, C++ and HTML. I'd be surprised if it were suitable to parse Javascript, too. And I don't know where you get "exact representation" from; the _whole point_ of them is that they are quite the opposite, and represent a _range_ of input data. – Lightness Races in Orbit Jan 11 '12 at 17:06
  • @LightnessRacesinOrbit: An exact defined range that is :) still, you haven't told us what else to use. Btw.: I do think it is possible to parse html in RegEx :) – Nikodemus RIP Jan 11 '12 at 17:23
  • 1
    @Nikodemus - If you think you can parse HTML in regex, I have a bridge to sell you. BTW, here's a nice poem for you: http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags#answer-1732454 – Joseph Silber Jan 11 '12 at 17:34
  • @Nikodemus: No, I don't have an alternative suggestion handy. And, no, it's not always an "exact defined range"; take a course on regular and non-regular languages. – Lightness Races in Orbit Jan 11 '12 at 18:09
1

Use:

console\.log\("([^\\"]|\\.)*"\);

This will eat each backslash with the next character, and eat no quote.

In action: http://regexr.com?2vmmt

Benoit
  • 76,634
  • 23
  • 210
  • 236
-1

The regex is:

console\.log\("[^"]*"\)
Milad Naseri
  • 4,053
  • 1
  • 27
  • 39