However, if the project does not use frameworks such as jQuery, and js itself does not have such functions, we have to write such functions ourselves. The following is the specific implementation of the function:
//For users to call
function trim(s){
return trimRight(trimLeft(s));
}
//Remove the blank on the left
function trimLeft(s){
if(s == null) {
return "";
}
var whitespace = new String(" \t\n\r");
var str = new String(s);
if (((0)) != -1) {
var j=0, i = ;
while (j < i && ((j)) != -1){
j++;
}
str = (j, i);
}
return str;
}
//Remove the blank on the right
function trimRight(s){
if(s == null) return "";
var whitespace = new String(" \t\n\r");
var str = new String(s);
if (((-1)) != -1){
var i = - 1;
while (i >= 0 && ((i)) != -1){
i--;
}
str = (0, i+1);
}
return str;
}
When using it, just call the trim function.
The following is the implementation method using regularity:
<SCRIPT LANGUAGE="JavaScript">
<!--
= function()
{
return (/(^\s*)|(\s*$)/g, "");
}
= function()
{
return (/(^\s*)/g, "");
}
= function()
{
return (/(\s*$)/g, "");
}
//-->
</SCRIPT>
<input type="text" value=" There are spaces before and after " >
<input type="button" value="get the front and back spaces" onclick="javascript:('space').value=('space').();('space').select();">
<input type="button" value="de-front space" onclick="javascript:('space').value=('space').();('space').select();">
<input type="button" value="return to the back space" onclick="javascript:('space').value=('space').();('space').select();">
<input type="button" value="restore" onclick="javascript:('space').value=' There are spaces before and after ';">
Due to editor problems, spaces have been replaced, so please pay attention to adding spaces and testing.
Copy the codeThe code is as follows:
//For users to call
function trim(s){
return trimRight(trimLeft(s));
}
//Remove the blank on the left
function trimLeft(s){
if(s == null) {
return "";
}
var whitespace = new String(" \t\n\r");
var str = new String(s);
if (((0)) != -1) {
var j=0, i = ;
while (j < i && ((j)) != -1){
j++;
}
str = (j, i);
}
return str;
}
//Remove the blank on the right
function trimRight(s){
if(s == null) return "";
var whitespace = new String(" \t\n\r");
var str = new String(s);
if (((-1)) != -1){
var i = - 1;
while (i >= 0 && ((i)) != -1){
i--;
}
str = (0, i+1);
}
return str;
}
When using it, just call the trim function.
The following is the implementation method using regularity:
Copy the codeThe code is as follows:
<SCRIPT LANGUAGE="JavaScript">
<!--
= function()
{
return (/(^\s*)|(\s*$)/g, "");
}
= function()
{
return (/(^\s*)/g, "");
}
= function()
{
return (/(\s*$)/g, "");
}
//-->
</SCRIPT>
<input type="text" value=" There are spaces before and after " >
<input type="button" value="get the front and back spaces" onclick="javascript:('space').value=('space').();('space').select();">
<input type="button" value="de-front space" onclick="javascript:('space').value=('space').();('space').select();">
<input type="button" value="return to the back space" onclick="javascript:('space').value=('space').();('space').select();">
<input type="button" value="restore" onclick="javascript:('space').value=' There are spaces before and after ';">
Due to editor problems, spaces have been replaced, so please pay attention to adding spaces and testing.