最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get last line of multiline text with matching regex? - Stack Overflow

programmeradmin3浏览0评论

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'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.

Share Improve this question asked Apr 1, 2018 at 16:21 user9582605user9582605 1
  • Use x.trim().split("\n").pop(). Or s.trim().match(/.*$/) – Wiktor Stribiżew Commented Apr 1, 2018 at 16:45
Add a ment  | 

4 Answers 4

Reset to default 6

[\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. 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.

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]);

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>

发布评论

评论列表(0)

  1. 暂无评论