JavaScript Remove the space before and after the instance _javascript tips

Source: Internet
Author: User
Tags rtrim

Code

Copy Code code as follows:

function String.prototype.Trim () {return this.replace (/^/s*) | (   /s*$)/g, ""); Remove left and right spaces
function String.prototype.Ltrim () {return this.replace (/(^/s*)/g, "");} Remove left space
function String.prototype.Rtrim () {return this.replace (/(/s*$)/g, "");} Remove Right space

Cases

<script type= "Text/javascript" >
function Trim (str) {//Remove the left and right ends of the space return   str.replace (/(^s*) | (   s*$)/g, "");
function LTrim (str) {//Remove the left space return   str.replace (/(^s*)/g, "");
function RTrim (str) {//Remove the right space return   str.replace (/(s*$)/g, "");
}
</script>      

Remove all spaces

<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>

JS Go String Space summary

STR is the string to remove the space:
Remove all spaces:
str = str.replace (/\s+/g, "");
Remove two spaces:
str = str.replace (/^\s+|\s+$/g, "");
Remove left space:
Str=str.replace (/^\s*/, "");
Remove right space:
Str=str.replace (/(\s*$)/g, "");

<script language= "JavaScript" > <!--/provenance: Online collection//Trim (), Ltrim (), RTrim () String.prototype.Trim = Func tion () {returnthis.replace (/^\s*) | (  
\s*$)/g, "");  
} String.prototype.LTrim = function () {Returnthis.replace (/(^\s*)/g, "");  
} String.prototype.RTrim = function () {Returnthis.replace (/(\s*$)/g, ""); }//--> </SCRIPT> <input type= "text" value= "All Spaces" id= "space" > <input type= "button" value= "Go before and after the space" onclick= "javascript:document.getElementById (' spaces '). Value=document.getelementbyid (' SPACEBAR '). Value. Trim ();d Ocument.getelementbyid (' Space '). Select (); " > <input type= "button" value= "go to the front space" onclick= "javascript:document.getElementById (' spaces '). value= document.getElementById (' space '). Value. LTrim ();d Ocument.getelementbyid (' Space '). Select (); " > <input type= "button" value= "to go 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 ';  >

Copy Code code as follows:

Remove all spaces:
str = str.replace (/\s+/g, "");
Remove two spaces:
str = str.replace (/^\s+|\s+$/g, "");
Remove space (Timesheet used)
Arg0=arg0.replace (/\s+$|^\s+/g, "");

Let's take a look at what "s/s represents in the JS script.
s matches any whitespace character, including spaces, tabs, page breaks, and so on. equivalent to [FNRTV].
Please bear in mind is the lowercase s, the other careful friend may see sometimes \s+ is the * number, you can see the following explanation

Commonly used qualifiers
Code/Syntax Description
* Repeat 0 or more times
+ Repeat one or more times
? Repeat 0 times or once
N Repeat n times
{N,} Repeat N or more times
{N,m} Repeat N to M times

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.