JavaScript regular trim remove spaces and strings

Source: Internet
Author: User
Tags rtrim

About trim in fact, nothing to say, is to remove the first space, for modern browsers is simply a regular/^\s+|\s+$/can be done.
JavaScript Regular expressions implement trim ()

The code is as follows Copy Code

String.prototype.trim = function () {
Return This.replace (/(^\s*) | ( \s*$)/, "");
}

Trimleft ()

String.prototype.trimleft=function () {
Return This.replace (/(^\s*)/, "");
}

TrimRight ()

String.prototype.trimRight () =funtion () {
Return This.replace (/(\s*$)/, "");
}

The following is an example of trim deletion space

The code is as follows Copy Code

<script language= "JavaScript" >
<!--
String.prototype.Trim = function ()
{
Return This.replace (/(^s*) | ( s*$)/g, "");
}
String.prototype.LTrim = function ()
{
Return This.replace (/(^s*)/g, "");
}
String.prototype.RTrim = function ()
{
Return This.replace (/(s*$)/g, "");
}
-->
</SCRIPT>
<input type= "text" value= "both before and after the space" id= "spaces" >
<input type= "button" value= "to the front and back spaces" onclick= "javascript:document.getElementById (' space '). value= document.getElementById (' space '). Value. Trim ();d Ocument.getelementbyid (' Space '). Select (); " >
<input type= "button" value= "to the front space" onclick= "javascript:document.getElementById (' spaces '). value= document.getElementById (' space '). Value. LTrim ();d Ocument.getelementbyid (' Space '). Select (); " >
<input type= "button" value= "After the space" onclick= "javascript:document.getElementById (' spaces '). value= document.getElementById (' space '). Value. RTrim ();d Ocument.getelementbyid (' Space '). Select (); " >
<input type= "button" value= "Restore" onclick= "javascript:document.getElementById (' space '). value= ' All spaces '; >

and support Chinese space &nbsp; Wait a minute.
What \s support Chinese space?
Yes.

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

Original:
Matches a single white spaces 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\u3 000].

Google translation:
Matches a single whitespace character, including spaces, tabs, page break, newline, and other Unicode spaces.
Equivalent \f\n\r\t\v\u00a0\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u2028\u2029\u202f\u205f\u3 000]

Where \u00a0 is the &nbsp; \u3000 is a Chinese space, the other is what, I do not know, interested can go to turn 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 now JS is equivalent to all white space characters.
That said, but the lower version has always been [\f\n\r\t\v], even the trim has wood, so we have to compatible with the lower version, we can not simply use/^\s+|\s+$/processing, to add Chinese space and &nbsp;
So to/^[\s\u3000\u00a0]+| [\s\u3000\u00a0]+$/just go, such commonly used to have, as for those \u2000 and so on, I do not know what, can add in as needed.
Commonly used is nothing more than the Chinese space and solid space.

Let's see what JQuery does with this.

Text

The code is as follows Copy Code

1.4.1
RTrim =/^ (\s|\u00a0) +| (\s|\u00a0) +$/g,

1.5.1, 1.6.1, 1.7.1
Trimleft =/^[\s\xa0]+/;
TrimRight =/[\s\xa0]+$/;

1.8.1, 1.9.1, 1.10.1, 1.11.1
RTrim =/^[\s\ufeff\xa0]+| [\s\ufeff\xa0]+$/g,

All right, 1.4-1.7 all the same, values remove normal spaces and solid spaces.
1.8-1.11 added a \ufeff, what is this thing?
JQuery annotations are written to make sure we trim BOM and NBSP (here's looking at for you, Safari 5.0 and IE)
Ensure the removal of BOM and &nbsp; (See your Safari 5.0 and IE)
What BOM? Why does the BOM appear?
This thing is generally not out of the people, why should we remove this?
PS: The BOM here is Byte order mark (Byte-order Mark), not clear, please read here byte order mark.

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

Let's take a look at the original trim and how we're going to get rid of the results:

Text Run

  code is as follows copy code

var RTrim =/ ^[\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 () +" ' ");

The results are clear and clean, indicating that trim is also supported in Chinese spaces.
Well, today we'll share a little bit of knowledge, see you tomorrow.

Good to have you for trim () use the regular to remove spaces and Chinese space to know how much, here we note that we just replace the small print in English can Oh, because this system does not support \ Oh.

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.