Regular Expression in the trim method of js strings

Source: Internet
Author: User

Trim has nothing to say about. It is nothing more than removing the first space. For modern browsers, it is just a simple regular expression/^ \ s + | \ s + $. It also supports Chinese spaces & nbsp; and so on. What \ s supports Chinese spaces? Yes. Open RegExp # character-classes and click \ s. Original article: Matches a single white space character, including space, tab, form feed, line feed and other Unicode spaces. equivalent to [\ f \ n \ r \ t \ v \ u00a0 \ u1680 \ u180e \ u2000 \ u2001 \ u2002 \ u2003 \ u2004 \ u2005 \ u2006 \ u2007 \ u2008 \ u2009 \ u200a \ u2028 \ u2029 \ u202f \ u205f \ u3000]. google Translate: matches a single blank character, including spaces, tabs, form feed, line breaks, and other Unicode spaces. Equivalent to [\ f \ n \ r \ t \ v \ u00a0 \ u1680 \ u180e \ u2000 \ u2001 \ u2002 \ u2003 \ u2004 \ u2005 \ u2006 \ u2007 \ u2008 \ u2009 \ u200a \ u2028 \ u2029 \ u202f \ u205f \ u3000] Where \ u00a0 is & nbsp; \ u3000 is a Chinese space, and I don't know what others are. If you are interested, you can flip the unicode table by yourself. We can see that this has already overturned our traditional regular expressions. In the past, we only knew that \ s is equivalent to [\ f \ n \ r \ t \ v]. but I do not know that the current JavaScript code is equivalent to all blank characters. However, the earlier version is always [\ f \ n \ r \ t \ v], and even trim is available. Therefore, if we want to be compatible with the earlier version, you cannot simply use/^ \ s + | \ s + $/for processing. Add Chinese spaces and & nbsp; therefore, You Need To/^ [\ s \ u3000 \ u00A0] + | [\ s \ u3000 \ u00A0] + $/. This is a common solution, I don't know what the \ u2000 and others are. You can add them as needed. The commonly used space is nothing more than Chinese space and entity space. Let's take a look at how jQuery handles this. Text // 1.4.1rtrim =/^ (\ s | \ u00A0) + | (\ s | \ u00A0) + $/g, // 1.5.1, 1.6.1, 1.7.1trimLeft =/^ [\ s \ xA0] +/; trimRight =/[\ s \ xA0] + $/; // 1.8.1, 1.9.1, 1.10.1, 1.11.1rtrim =/^ [\ s \ uFEFF \ xA0] + | [\ s \ uFEFF \ xA0] + $/g, okay, 1.4-1.7 is the same, remove common spaces and entity spaces. 1.8-1.11 adds a \ uFEFF. What is this? JQuery annotations are written to Make sure we trim BOM and NBSP (here's looking at you, Safari 5.0 and IE; (See your Safari 5.0 and IE.) What BOM? Why does BOM appear? This is something that most people cannot do. Why should we remove it? PS: the BOM here is a byte-order mark. It is unclear. Please refer to the byte sequence mark here. I don't know. I can't go over the wall recently, so I'm too lazy to find it. However, if he does not remove Chinese spaces, it is a bit difficult to say. Do they ignore Chinese spaces if they do not know Chinese? So we should optimize this regular expression/^ [\ s \ u3000 \ uFEFF \ xA0] + | [\ s \ u3000 \ uFEFF \ xA0] + $/g. Let's take a look at the native trim and our regular expression removal results: run var rtrim =/^ [\ s \ u3000 \ uFEFF \ xA0] + | [\ s \ u3000 \ uFEFF \ xA0] + $/g; console. log ("common space test:"); console. log ("'" + "Common Space ". replace (rtrim, "") + "'"); console. log ("'" + "Common Space ". trim () + "'"); console. log ("entity space test:"); console. log ("'" + "\ u00a0 entity space \ u00a0 ". replace (rtrim, "") + "'"); console. log ("'" + "\ u00a0 entity space \ u00a0 ". trim () + "'"); console. log ("Chinese space test:"); console. l Og ("'" + "Chinese space \ u3000 ". replace (rtrim, "") + "'"); console. log ("'" + "Chinese space \ u3000 ". trim () + "'"); the results are obvious, all are removed, indicating that trim also supports Chinese spaces. Now let's share this small point of knowledge today. See you tomorrow.

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.