Home  >  Article  >  Web Front-end  >  How to remove spaces in javascript

How to remove spaces in javascript

藏色散人
藏色散人Original
2021-04-01 14:35:434183browse

Javascript method to remove spaces: 1. Use the "str.replace(/(^\s*)|(\s*$)/g, "");" method to remove all spaces before and after the string ;2. Use "Trim(str,is_global)..." to remove all spaces in the string.

How to remove spaces in javascript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

How to use JS to remove spaces before and after a string or to remove all spaces

This article mainly introduces how to use JS to remove spaces before and after a string or to remove all spaces. You need Friends can refer to

1. Remove all spaces before and after the string:

The code is as follows:

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

Description:

If you use jQuery, you can directly use the $.trim(str) method. str represents a string in which all spaces before and after are to be removed.

2. Remove all spaces in the string (including intermediate spaces, you need to set the second parameter to: g)

The code is as follows:

 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 the trim function of strings. However, in order to be compatible with browsers that do not support it, it is best to add the following code to the Js file (no need to clear newlines) Please delete \n delete the tab character\t):

if (!String.prototype.trim) {
 
 /*---------------------------------------
  * 清除字符串两端空格,包含换行符、制表符
  *---------------------------------------*/
 String.prototype.trim = function () {
  return this.triml().trimr();
 }
 
 /*----------------------------------------
  * 清除字符串左侧空格,包含换行符、制表符
  * ---------------------------------------*/
 String.prototype.triml = function () {
  return this.replace(/^[\s\n\t]+/g, "");
 }
 
 /*----------------------------------------
  * 清除字符串右侧空格,包含换行符、制表符
  *----------------------------------------*/
 String.prototype.trimr = function () {
  return this.replace(/[\s\n\t]+$/g, "");
 }
}

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

if (!String.prototype.trim){
 
 /*---------------------------------------
  * 清除字符串两端空格,包含换行符、制表符
  *---------------------------------------*/
 String.prototype.trim = function () {
  return this.replace(/(^[\s\n\t]+|[\s\n\t]+$)/g, "");
 }
  
}

Use code:

var str = " abcd ".trim();

[Recommended learning: js basic tutorial]

The above is the detailed content of How to remove spaces in javascript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn