Skip to content
Advertisement

How to replace a full console logs with preceding whitespaces and possible new line from string with regex

I need to replace a console.log('') construction in a string using regex. Before the console log there can be random number of whitespaces and one or zero new line characters ‘n’. I want to delete it together with the console log. Here is my code:

JavaScript

The desired result is <script>n</script>. When I test the regex in the tester it works however it does not get replaced.

Thanks

Advertisement

Answer

You can use

JavaScript

See the online demo

Output:

JavaScript

Details:

  • s+ – one or more whitespaces
  • console.log( – a literal console.log( text
  • .* – any zero or more chars other than line break chars as many as possible
  • ) – a ) char
  • ;? – an optional ; char.
Advertisement