Skip to content
Advertisement

How do you delete every line inside of a string that doesn’t contain a specific word?

There’s a string with multiple lines of random characters, I’d like to delete every line that doesn’t contain the word

:tesTWORD:

For example, this part of the original string

JavaScript

Would turn into this:

JavaScript

So basically all lines that don’t contain the exact word :tesTWORD: get deleted.

I have tried a bunch of different things like playing around with arrays, but nothing worked like it’s supposed to

Advertisement

Answer

You can use n to split it into an array,then filter the array,finally using join() to form a new string

JavaScript

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement