Crop the string trim () and customize the layout _ basic knowledge

Source: Internet
Author: User
The following describes the problems encountered by the custom trim () function and the improvement process. We recommend that you use the native function ECMAScript5 in the supported browsers to define the native trim method for the string. This method may be faster than any version in this article. We recommend that you use native functions in supported browsers. The following describes the problems encountered by the custom trim () function and the improvement process. Kung fu is still refined in order to be mellow.

JavaScript does not have a native trim method to remove the leading and trailing spaces of a string. The most common custom trim () function implementation is as follows:

The Code is as follows:


Function trim (text ){

Return text. replace (/^ \ s + | \ s + $/g ,'');

}

This method uses a regular expression to match one or more spaces at the beginning and end of a string. The replace () method replaces all matched parts with a null string.

However, this implementation method has a performance problem based on regular expressions. This effect comes from two aspects: one is to specify the pipeline operators with two matching modes, on the other hand, it indicates the g tag that globally applies this mode.

Taking this into consideration, we can split the regular expression into two parts and remove the g tag to rewrite the function, slightly improving its speed.

The Code is as follows:


Function trim (text ){

Return text. replace (/^ \ s +/, ''). replace (/\ s + $ /,'');

}

Another improved version. Make sure that the regular expression is as simple as possible.

The Code is as follows:


Function trim (text ){

// Delete the blank header of the string

Text = text. replace (/^ \ s + /,'');

// Clear trailing spaces cyclically

For (var I = text. length; I --;){

If (/\ S/. test (text. charAt (I) {// \ S non-blank characters

Text = text. substring (0, I + 1 );

Break;

}

}

Return text;

}

Suggestion: the performance of 2nd trim () functions is good when processing short strings on a small scale. The 3rd trim functions are much faster in processing long strings.

ASIDE: the simple function of cropping the first and last blank characters of a string triggers consideration of the performance of regular expressions and avoids performance problems. The pursuit of perfection in technology can only go forward in practice.

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.