How to delete empty rows using regular expressions in Javascript

Source: Internet
Author: User

Our goal is to delete empty rows in the text, which may be one or more rows.

Therefore, my operations are as follows:

Here, we use

(\ R \ n )*

In this way, multiple empty rows can be matched, and null can be used to replace the null rows. This solution is based on the use of \ r and \ n.

In the past, we saw an implementation on the Internet:

\ N [\ s |] * \ r

After matching, use null instead. You can also delete empty rows, as shown in the following figure:

Both solutions can achieve the same effect.

If you have other different understandings, Please comment and make progress together.

I did the following tests again, for example:

Used only

\ N \ r

This completely achieves the deletion of all empty rows, because at the beginning and end of each empty row is

\ N \ r

Therefore, after the previous row is deleted, the current row has a \ n \ r, which can also be matched. Therefore, an alternative operation must be performed. In this way, all empty rows can be deleted.

Then, if it is

\ R \ n. What is the effect?

I did another test. The results are as follows:

This is not the result we want. Why?

I think we can understand it like this,

C: \ Program Files \ Common Files \ Relive. dll

C: \ Program Files \ Common Files \ svchost.exe

C: \ Program Files \ Internet Explorer \ msvcrt. bak

C: \ Program Files \ Internet Explorer \ msvcrt. dll

C: \ Program Files \ Internet Explorer \ msvcrt. ebk

C: \ WINNT \ system32 \ drivers \ npf. sys

C: \ WINNT \ system32 \ wpcap. dll

C: \ WINNT \ system32 \ Packet. dll

C: \ WINNT \ system32 \ WanPacket. dll

C: \ Documents ents and Settings \ User name \ Local Settings \ Temp \ wmso.exe

C: \ Documents ents and Settings \ User name \ Local Settings \ Temp \ BCG5.tmp

This is the original text. In fact, when we process the text, we can use

\ R \ n (line feed, line feed first, and then line feed)

To replace the text:

C: \ Program Files \ Common Files \ Relive. dll \ r \ n \ r \ nC: \ Program Files \ Common Files \ svchost.exe \ r \ n \ r \ nC: \ Program Files \ Internet Explorer \ msvcrt. bak \ r \ nC: \ Program Files \ Internet Explorer \ msvcrt. dll \ r \ n \ nC: \ Program Files \ Internet Explorer \ msvcrt. ebk \ r \ n \ r \ nC: \ WINNT \ system32 \ drivers \ npf. sys \ r \ nC: \ WINNT \

System32 \ wpcap. dll \ r \ nC: \ WINNT \ system32 \ Packet. dll \ r \ nC: \ WINNT \ system32 \ WanPacket. dll \ r \ n \ r \ nC: \ Documents ents and Settings \ User name \ Local Settings \ Temp \ wmso.exe \ r \ nC: \ Documents and Settings \ User name \ Local Settings \ Temp \ BCG5.tmp \ r \ n

This section of text is a whole, with no carriage return, no gaps and intervals. This is the text we are processing, but the carriage return line breaks are invisible characters, so we cannot see it, but it still exists.

Therefore, the above situation will only occur during processing.

Here, we can understand why

After matching and replacing with \ r \ n,

All \ r \ n are lost. Therefore, the text becomes a connected plain character text.

However

\ N \ r

After replacement,

All \ n \ r in the middle are replaced, \ r (\ n \ r ...... \ N \ r) \ n, the middle part is replaced. So the last \ r \ n is left.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.