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 |