javascript - regex to remove last line of block of text- Stack Overflow

how do i remove the last line from a block of text in java script without using loops.(only using regex

how do i remove the last line from a block of text in java script without using loops.(only using regex) eg.,

"The Internet is a global system of interconnected puter networks that use the standard Internet Protocol Suite (TCP/IP) to serve billions of users worldwide. It is a network of networks that consists of millions of private and public, academic, business, and government networks&&".

The last line always have "&&".

how do i remove the last line from a block of text in java script without using loops.(only using regex) eg.,

"The Internet is a global system of interconnected puter networks that use the standard Internet Protocol Suite (TCP/IP) to serve billions of users worldwide. It is a network of networks that consists of millions of private and public, academic, business, and government networks&&".

The last line always have "&&".

Share Improve this question asked Nov 19, 2009 at 5:04 SidharthSidharth 3,6797 gold badges29 silver badges24 bronze badges 6
  • 1 "last line" as in last sentence? – Crescent Fresh Commented Nov 19, 2009 at 5:08
  • @Sidharth: would you be able to show us the result you are expecting ? – RageZ Commented Nov 19, 2009 at 5:08
  • Are you asking how to remove the last sentence or just the "&&" or literally the last line (I think that would be something like this "\n.*&&$")? – zfedoran Commented Nov 19, 2009 at 5:10
  • 1 @ RageZ ,i am expecting "millions of private and public, academic, business, and government networks&&" should be removed from the above block .. – Sidharth Commented Nov 19, 2009 at 5:14
  • @ Arriu , i need to remove last line not just "&&" . Its just a clue that last line always have "&&"... – Sidharth Commented Nov 19, 2009 at 5:15
 |  Show 1 more ment

5 Answers 5

Reset to default 4
var str = "The ... and government networks&&";
str.replace(/\.[^.]+&&$/,'.');

If by last line you mean last sentence. And the last sentence doesn't contain a period.

Your text has no newlines in it, so I guess you mean to remove the last sentence-

string.replace(/\.[^\.]+\.?$/,'.');

If you want to be sure not to remove the only sentence, use

string.replace(/(\S+\.)[^\.]+\.?$/,'$1')

You will need to some heavier lifting to avoid cutting off lines that have abbreviations with periods, like Mr. Jones, Dr. Spock, Mrs. Jones and St. Andrew. Like removing their periods before the amputation and replacing them after. (split here for readibility)

string= string.replace(/\b(Mr|Dr|Mrs|St)\. /g,'$1 ')
.replace(/(\S+\.)[^\.]+\.?$/,'$1').replace(/\b(Mr|Dr|Mrs|St) +/g,'$1. ')

if you need the new line has "puter new line"

var str = "he ... and government networks&&";
str.replace(/\n[^\n]*&&$/,'');

Like this?

   "First Line\n.sth.&&.\nAnother\nLast Line&&Last Word".replace(/\n.*&&.*$/,'');

Result

First Line
..sth.&&.
Another

According to your question, The last line always have "&&"., it does not mean that must be end with &&, right? and other lines can have && too.

If your text has no line breaks, a "last line" does not exist. It only exists when it is displayed in the browser, but not as part of the text. Although you insist on "only using regex", a regex cannot find it.
You can get away with it if you use a fixed width font, but that is unlikely. You can also loop until you reach the target height, but this is undesired.

A better approach here it to use CSS. Here's a simplified version that shows the first two lines and hides the rest:

p.TwoLines {
   overflow: hidden; /* no scroll bars */
   font-size: 10pt; /* here I have a fixed size, for example */
   line-height: 1.5;
   height: 30pt /* 1.5*2lines *10sizeofline */;

   width: 300px; /* not needed, just to see the effect */
}

Preview: http://jsbin./ufihe3

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745162741a4614464.html

相关推荐

  • javascript - regex to remove last line of block of text- Stack Overflow

    how do i remove the last line from a block of text in java script without using loops.(only using regex

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信