On the regular article of JS string trim method

Source: Internet
Author: User
Tags rtrim

About trim actually nothing to say, is to remove the first space, for the modern browser is just simple regular /^\s+|\s+$/ can be done.
and support Chinese space   Wait a minute.
What \s support Chinese spaces?
Yes.

Open regexp#character-classes down a bit and find \s this explanation.

Original:
Matches a single white space character, including space, tab, form feeds, line feeds 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 translation:
Matches a single white space character, including spaces, tabs, page breaks, 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]

One of the \u00a0 is   \u3000 is a Chinese space, the other is what, I do not know, interested can go through the Unicode table.

See this, has overturned our traditional regular norms, previously, we only know \s equivalent to [\f\n\r\t\v], but do not know that the current JS is equivalent to all the white space characters.
That being said, but the lower version has always been [\f\n\r\t\v], even trim all wood has, so we want to be compatible with the lower version, can not be easily handled with /^\s+|\s+$/ , to add Chinese space and  
So to /^[\s\u3000\u00a0]+|[ \s\u3000\u00a0]+$/ only, so commonly used to have, as for those \u2000 and so on, I do not know what is, can be added on demand.
Commonly used is nothing more than the Chinese space and solid space.

Let's see what JQuery does with this.

Text
//1.4.1 /^ (\s|\u00a0) +| (\s|\u00a0) +$/g,//1.5.1, 1.6.1, 1.7.1/^[\s\xa0]+//[\s\xa0]+$/; //1.8.1, 1.9.1, 1.10.1, 1.11.1 /^[\s\ufeff\xa0]+| [\s\ufeff\xa0]+$/g,
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.1RTR im =/^[\s\ufeff\xa0]+| [\s\ufeff\xa0]+$/g,

All right, 1.4-1.7 are the same. Values remove normal spaces and solid spaces.
1.8-1.11 added a \ufeff, what is this thing?
JQuery notes written to make sure we trim BOM and NBSP (here's looking at you, Safari 5.0 and IE)
Ensure removal of BOM and   (See your Safari 5.0 and IE)
What BOM? Why is BOM appearing?
This thing can not be out of the ordinary people, why do you want to remove this?
PS: Here's the BOM is the byte order mark (Byte-order Mark), not clear, please flip through here byte order mark.

I don't know, I can't turn over the wall recently, so I'm too lazy to find it.
But he does not remove the Chinese space this is a bit unreasonable, they can not Chinese, ignore the Chinese space?
So we should optimize this regular /^[\s\u3000\ufeff\xa0]+|[ \s\u3000\ufeff\xa0]+$/g that's right.

Look at the native trim and we're going to get rid of the results:

Text Run
varRTrim =/^[\s\u3000\ufeff\xa0]+| [\s\u3000\ufeff\xa0]+$/g; Console.log ("Normal space test:"); Console.log ("'"+"Normal space". replace (RTrim,"") +"'"); Console.log ("'"+"Normal 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.log ("'"+"Chinese space \u3000". replace (RTrim,"") +"'"); Console.log ("'"+"Chinese space \u3000". Trim () +"'");
var RTrim =/^[\s\u3000\ufeff\xa0]+| [\s\u3000\ufeff\xa0]+$/g;console.log ("Normal space Test:"); Console.log ("'" + "plain Space". Replace (RTrim, "") + "'"); Console.log ("'" + "normal spaces". Trim () + "'"), Console.log ("Entity Space Test:"), Console.log ("'" + "\u00a0 entity space \u00a0". Replace (RTrim, "") + "'"); co Nsole.log ("'" + "\u00a0 entity space \u00a0". Trim () + "'"); Console.log ("Chinese Space Test:"); Console.log ("'" + "Chinese space \u3000". Repl Ace (RTrim, "") + "'"), Console.log ("'" + "Chinese space \u3000". Trim () + "'");

As a result, it is clear that the trim is also supported in Chinese spaces.
Well, share this little bit of knowledge today and see you tomorrow.

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.