views:

166

answers:

3

In javascript, If i have a text block like so

Line 1
Line 2
Line 3

What would i need to do to lets say delete the first line and turn it into:

Line 2
Line 3
+1  A: 

In a nutshell: Look for the first line return (\n) and use the JavaScript replace function to remove everything up to it (and including it.)

Here is a RegEx that does it (surprisingly tricky, at least for me...)

<script type = "text/javascript">
var temp = new String('Line1\nLine2\nLine3\n');
temp = temp.replace(/[\w\W]+?\n+?/,"");
alert (temp);
</script>
LesterDove
Since you're using the /g paramater ("global replace"), I believe this expression will have the effect of deleting all of the lines (except the last one, if it isn't newline-terminated).
Dan Story
I noticed that, thanks. Problems with the proverbial scissors over here...
LesterDove
I think the question was more looking to remove a *specific line* rather than just the first line. This solution doesn't really work too well for deleting arbitrary lines by number, or by content.
ShZ
+4  A: 

The cleanest way of doing this is to use the split and join functions, which will let you manipulate the text block as an array of lines, like so:

// break the textblock into an array of lines
var lines = textblock.split('\n');
// remove one line, starting at the first position
lines.splice(0,1);
// join the array back into a single string
var newtext = lines.join('\n');
Dan Story
you could even do it in one line: `textblock.split("\n").slice(1).join("\n")`
nickf
A: 
var firstLineRemovedString = aString.replace(/.*/, "").substr(1);
Eli Grey