I need to use JavaScript to remove blank lines in a HTML text box. The blank lines can be at anywhere in the textarea
element. A blank line can be just a return or white spaces plus return.
I am expecting a regular expression solution to this. Here are some I tried, but they are not working and cannot figure out why:
/^s*
?
/g
/^s*
?
$/g
Edit 1
It appears that the solution (I modified it a little) suggested by aaronman and m.buettner works:
string.replace(/^s*
/gm, "")
Can someone tell why my first regular expression is not working?
Edit 2
After reading all useful answers, I came up with this:
/^[s]*(
|
|
)/gm
Is this going to be one that cover all situations?
Edit 3
This is the most concise one covering all spaces (white spaces, tabs) and platforms (Linux, Windows, Mac).
/^s*[
]/gm
Many thanks to m.buettner!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…