8

Let's say I have text (not html), that I'm pulling from a textarea. It looks like:

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'm trying to delete from ALTER to GO for each user. With replace(), I can replace from ALTER to DISABLE, but I can't quite figure out how to match all the way to GO (which is on the next line), so that it removes the whole chunk. Thoughts?

2
  • had you consider replacing the text using regular expressions? Commented May 4, 2011 at 14:11
  • I'm pretty horrible with regx's, definitely something I need to work on. Thanks for the answer Alex! Commented May 4, 2011 at 14:14

1 Answer 1

20

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

Instead, you can use the [\s\S] character class, which matches all whitespace and all non whitespace, which is everything. The ? after * means it won't be greedy, otherwise it will match between the first ALTER and the last GO.

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

jsFiddle.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.