Regular Expressions for removing spaces and line breaks in JS (recommended)

Source: Internet
Author: User
The following small series will provide you with a regular expression (recommended) for removing spaces and line breaks in JavaScript ). I think this is quite good. Now I will share it with you and give you a reference. Let's take a look at the small Editor. When programming a few days ago, I encountered a problem that I had to solve for a long time. It really drove me crazy! Put a string in setTimeout and there is no way to execute it. Later it was found that there was a line break behind the string. If you don't take a closer look, you can use a regular expression to remove the line break.

// Remove the space String. prototype. trim = function () {return this. replace (/\ s +/g, "") ;}// remove the wrap function ClearBr (key) {key = key. replace (/<\/?. +?> /G, ""); key = key. replace (/[\ r \ n]/g, ""); return key;} // remove the left space function LTrim (str) {return str. replace (/^ \ s */g, "") ;}// remove right space function RTrim (str) {return str. replace (/\ s * $/g, "") ;}// remove the space at both ends of the string function trim (str) {return str. replace (/(^ \ s *) | (\ s * $)/g, "");} // remove the space in the string. function CTim (str) {return str. replace (/\ s/g, '');} // whether the string is_digitals (str) is composed of digits) {var reg =/^ [0-9] * $/; // match the return reg. test (str );}

Now I find myself more and more fond of using regular expressions! It is simple and intuitive. Of course, you are familiar with regular expressions. I also tried to write this JS method to delete the line feed myself, which really made me implement it!

The above JS Regular Expressions (recommended) for removing spaces and line breaks are all the content shared by the editor. I hope to give you a reference and support for PHP.

For more articles on JS Regular Expressions (recommended) for removing spaces and line breaks, please follow the PHP Chinese website!

Related Article

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.