简体   繁体   中英

How to get last line of multiline text with matching regex?

I'm looking for hours now how to get last line of a multilines text.

For example with :

Lorem ipsum dolor sit amet, 
Praesent libero. Sed cursus ante 
dapibus diam. Sed nisi. Nulla  
imperdiet. Duis sagittis ipsum. 

I would get :

imperdiet. Duis sagittis ipsum.

I tried with a regex like :

var y = x.match(/[\S]+$/);

But it returns :

null

How can I get last line of a multiline text with a regex in javascript?

Thank you.

I am not good with Regex.

However, to answer this, you can use split('\\n')

var myText = <your multi line text>;
var lines = myText.split('\n');
console.log(lines[lines.length - 1]); // This should print last line

As per your JSFiddle, you have to take second last line as the last line is EMPTY

var x=document.getElementsByTagName('p')[0].innerHTML;
var y = x.split('\n');
alert(y[y.length - 2]);

[\\S]+$ (which is long for \\S+$ ), means to matches all non-whitespace at the end of the string.

Since your string has a space at the end of the last line, it doesn't match anything.

Even if you removed that trailing space, it would only match ipsum.

Simply use: .*$

This relies on the fact that . doesn't match line-break characters by default ( (?s) can change that, when needed) .

So it matches all characters from the last line-break until the end of the string.

See regex101.com for demo.


Now, if the text has one or more line-breaks after that last line, so it isn't really the last line, use .*\\s*$ instead. See demo .

To not capture blank lines at the end, use a capture group to catch the last non-blank line: (.*)\\s*$ . See demo .

Using regex, you can do this

var text = your text goes here;
var regex = /\n.*$/;
match = regex.exec(text);
console.log(match[0].slice(1));

Check for the match that starts with \\n (newline character), and then ends with something else than \\n , that will get you the last line. Finally just remove the first character from the match as it will be the starting \\n (last character from the previous line).

Note that this will work only if the text is actually multiline - contains \\n - so you should also test it to begin with.

if (text.indexOf('\n') == -1) return text;

you should use (.+)([^\\s]) to ignore the last line if empty :

For multi paragraphs :

 var u = document.getElementsByTagName('p'); for (var i = 0; i < u.length; i++) { var z = u[i].innerHTML.match(/(.+)([^\\s])/g); console.log(z[z.length - 1]); } 
 <p> Lorem ipsum dolor sit amet, Praesent libero. Sed cursus ante dapibus diam. Sed nisi. Nulla imperdiet. Duis sagittis ipsum. </p><p> Lorem ipsum dolor sit amet, Praesent libero. Sed cursus ante dapibus diam. Sed nisi. Nulla imperdiet. Duis sagittis ipsum. </p><p> Lorem ipsum dolor sit amet, Praesent libero. Sed cursus ante dapibus diam. Sed nisi. Nulla imperdiet. Duis sagittis ipsum. </p><p> Lorem ipsum dolor sit amet, Praesent libero. Sed cursus ante dapibus diam. Sed nisi. Nulla imperdiet. Duis sagittis ipsum. </p> 

For single one :

 var x=document.getElementsByTagName('p')[0].innerHTML; var y = x.match(/(.+)([^\\s])/g); console.log(y[y.length -1]); 
 <p>Lorem ipsum dolor sit amet, Praesent libero. Sed cursus ante dapibus diam. Sed nisi. Nulla imperdiet. Duis sagittis ipsum. </p> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM