Regular replace newline and replace BR with newline character _ regular expression

Source: Internet
Author: User

Replace newline and replace <br/> with newline character.

Example: str = str.replace (/\r\n/g, "<br/>");//replace line breaks with <br/> note: \ r \ n A shortcut to a regular expression cannot be/r/n or \/r\/n

str = str.replace (/<br\/>g, "\ r \ n");//NOTE: The reserved word for the regular expression must be preceded by \ Escape

Another point:

/n or/r/n or/R may be line breaks, depends on whether your system is windows or Unix/linux or Mac
Alternatively,/s+ can be replaced under Windows, but/s+ will also match other characters, not a safe approach.

The cloud-dwelling community supplements:

In practical applications, you can not guarantee that the customer input is <br/> can also be <br> <br/> <BR> and other combinations. Then we need to use the following code to provide compatibility

Copy Code code as follows:

Dxycode=dxycode.replace (/<br\s*\/?>/gi, "\ r \ n");

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.