JS remove the space before and after the string or remove all spaces

Keywords: Javascript JQuery Firefox Google

From script house:

This article mainly introduces the use of JS to remove the spaces before and after strings or all spaces. Please refer to the following for your friends:

The code is as follows:

function Trim(str)
 { 
  return str.replace(/(^\s*)|(\s*$)/g, ""); 
}

Description:

If you use jQuery to directly use the $. trim(str) method, str represents the string to remove all spaces before and after.

2. Remove all spaces in the string (including the middle space. The second parameter needs to be set as: g)

function Trim(str,is_global)
  {
   var result;
   result = str.replace(/(^\s+)|(\s+$)/g,"");
   if(is_global.toLowerCase()=="g")
   {
    result = result.replace(/\s/g,"");
    }
   return result;
}

3. Most browsers now basically support string trim functions, but in order to be compatible with unsupported browsers, we'd better add the following code to the Js file (please delete the tab character if you don't need to clear the line break character to delete i t):

  

if (!String.prototype.trim) {
 
 /*---------------------------------------
  * Clear the spaces at both ends of the string, including line breaks and tabs
  *---------------------------------------*/
 String.prototype.trim = function () { 
  return this.triml().trimr(); 
 }
 
 /*----------------------------------------
  * Clear the space to the left of the string, including line breaks and tabs
  * ---------------------------------------*/
 String.prototype.triml = function () {
  return this.replace(/^[\s\n\t]+/g, "");
 }
 
 /*----------------------------------------
  * Clear the space to the right of the string, including line breaks and tabs
  *----------------------------------------*/
 String.prototype.trimr = function () {
  return this.replace(/[\s\n\t]+$/g, "");
 }
}

If you only need the trim function, you can write one:

if (!String.prototype.trim){
 
 /*---------------------------------------
  * Clear the spaces at both ends of the string, including line breaks and tabs
  *---------------------------------------*/
 String.prototype.trim = function () { 
  return this.replace(/(^[\s\n\t]+|[\s\n\t]+$)/g, "");
 }
  
}

Use code:

var str = " abcd ".trim();

Cause: I'm going to find a simple way to remove the space in js. I'm going to look through the documents and wonder if there's a javascript document in w3cschool. I'm going to Google it. I find that there's still a lot of support for trim() function in JQuery in firefox, but it's not supported in IE. the main reason is that the writing method is wrong. Here's the wrong writing method

var content = $('#content').val();    

if(content.trim() == '') {

  alert('empty');    

}

The above method will not report an error under firefox, but it will report an error under ie
The correct way of writing should be

var content = $('#content').val();    

if($.trim(content) == '')  {

  alert('empty');  

}

var content = $('#content').val();    

if(jQuery.trim(content) == '') {

  alert('empty');  

}

For a small problem, it's better to prepare a small tool js to solve these trivial problems. Fuzzy query doesn't remove the space and wants to be done in the front end.

 

Original address: http://www.jb51.net/article/109522.htm

Posted by codeDV on Sun, 03 May 2020 00:01:51 -0700