String.replace () text spanning multiple lines?

Say I have text (not html) that I pull from a text box. It looks like this:

ALTER LOGIN [user1] DISABLE GO ~~~~~~~~~~~~~ important stuff to keep ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~ important stuff to keep ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~ important stuff to keep ~~~~~~~~~~~~~~~ ALTER LOGIN [user2] DISABLE GO ~~~~~~~~~~~~~ important stuff to keep ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~ important stuff to keep ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~ important stuff to keep ~~~~~~~~~~~~~~~ 

I am trying to remove from ALTER for GO for each user. With the replacement (), I can replace ALTER with DISABLE, but I can’t figure out how to match all the GO methods (which is on the next line), so that it deletes the whole fragment. Thoughts?

+6
source share
1 answer

. in a regular expression matches every character except \n . In some regex variants, you can add the s flag to match them, but not in Javascript.

Instead, you can use the character class [\s\S] , which matches all spaces and all without spaces, which is all. ? after * means that it will not be greedy, otherwise it will match between the first ALTER and the last GO .

 str = str.replace(/ALTER[\s\S]*?GO/g, ''); 

jsFiddle .

+17
source

Source: https://habr.com/ru/post/887281/


All Articles