Jump to content

RegExp: replacing ONLY blank lines at the beginning of text?


pstein

Recommended Posts

Assume I have a text consisting of multiple (many) lines (e.g. in a textarea).

 

I want to eleminate all blank lines but only those at the beginning o the text.

 

How do I specify the corresponding regular expression?

 

.......replace(/s*n/g,"");

 

replaces ALL blank lines (even if they are later embedded in text).

 

Peter

 

Link to comment
Share on other sites

 

.......replace(/s*n/g,"");

The 'g' in your example is a modifier. It causes the regex to match all occurrences of a pattern. In this instance, every new line character and any preceding whitespace will get replaced by nothing, throughout the entire string.

 

Remove the 'g' and it will match the first occurrence only. You may also want to remove the 'n' because 's' matches all whitespace, including new lines.

 

So, your regex might need to look simply like...

string.replace(/s*/,"");

Does this work for you?

Link to comment
Share on other sites

Or will that just replace the first whitespace character?

 

The issue is that you want it to be global enough to replace any number of leading blank or whitespace lines.

Edited by davej
Link to comment
Share on other sites

(/s*/,"") didn't work for me in FF24.

<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><title>replace leading blank lines</title><style>#in1,input{display:block;float:none;clear:both;}#rule{width:10px;float:left;background-color:#eee;margin-top:10px;padding-right:10px;}#out1{float:left;margin-top:10px;}</style><script>var p = {};//project namespacep.init = function() {p.addHandler(document.getElementById('btn1'),'click',p.evaluate1);p.addHandler(document.getElementById('btn1b'),'click',p.evaluate1b);p.addHandler(document.getElementById('btn2'),'click',p.evaluate2);p.addHandler(document.getElementById('btn3'),'click',p.evaluate3);}p.evaluate1 = function() {var txt = document.getElementById('in1').value;var txt2 = "";var ta = txt.split("n");var done = false;var i=0;var len=ta.length;var test;while(i<len){if(done && i<len-1){txt2 += ta[i] +"n";}else if(done && i==len-1){txt2 += ta[i];}else if(ta[i].trim().length!=0 && i<len-1){txt2 += ta[i] +"n";done = true;}else if(ta[i].trim().length!=0 && i==len-1){txt2 += ta[i];done = true;}i++;}txt2 = txt2.replace(/n/g,"<br/>");document.getElementById('out1').innerHTML = '['+ txt2 +']';}p.evaluate1b = function() {var txt = document.getElementById('in1').value;var txt2 = "";var ta = txt.split("n");var done = false;var i=0;var len=ta.length;var test;while(i<len){if(done){txt2 += "n"+ ta[i];}else if(ta[i].trim().length!=0){txt2 += ta[i];done = true;}i++;}txt2 = txt2.replace(/n/g,"<br/>");document.getElementById('out1').innerHTML = '['+ txt2 +']';}p.evaluate2 = function() {var txt = document.getElementById('in1').value;var txt2 = txt.replace(/^s*n/g,"");txt2 = txt2.replace(/n/g,"<br/>");document.getElementById('out1').innerHTML = '['+ txt2 +']';}p.evaluate3 = function() {var txt = document.getElementById('in1').value;var txt2 = txt.replace(/s*/,"");txt2 = txt2.replace(/n/g,"<br/>");document.getElementById('out1').innerHTML = '['+ txt2 +']';}p.addHandler = function(element,event,funct){if (element.addEventListener){element.addEventListener(event,funct,false);}else if (element.attachEvent){element.attachEvent(event,funct);//IE7,IE8}else{element['on'+event] = funct;if(element=='window' && event=='load'){alert('Your Browser is Obsolete');}}}p.addHandler(window,'load',p.init);</script></head><body><p>Remove leading blank lines but preserve other blank lines</p><textarea id="in1" rows="8" cols="60"></textarea><input type="button" id="btn1" value="Split Method"/><input type="button" id="btn1b" value="Improved Split Method"/><input type="button" id="btn2" value="Regex Replace Method 1"/><input type="button" id="btn3" value="Proposed Regex Method"/><div id="rule">1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0</div><div id="out1"></div></body></html>
Edited by davej
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...