Home  >  Article  >  Web Front-end  >  Detailed explanation of JS regular replacement method to remove spaces

Detailed explanation of JS regular replacement method to remove spaces

黄舟
黄舟Original
2017-03-25 14:45:481300browse

This article mainly introduces the method of JSregular replacement to remove spaces. It compares and analyzes the deletion techniques for full-width and half-width spaces in the form of examples. It involves the use of replace regular replacement. Friends who need it can Refer to the following

The example of this article describes the method of JS regular replacement to remove spaces. I share it with you for your reference. The details are as follows:

I have been searching online for a long time and found few that are easy to use. Make a backup copy of your own to save looking for it later.

//去左空格;
function ltrim(s){
  return s.replace( /^/s*/, "");
}
//去右空格;
function rtrim(s){
  return s.replace( //s*$/, "");
}
//左右空格;
function trim(s){
  return rtrim(ltrim(s));
}

If you remove the half-width and full-width spaces and replace /s with [" "|" "], it becomes

//去左空格;
function ltrim(s){
  return s.replace( /^[" "|" "]*/, "");
}
//去右空格;
function rtrim(s){
  return s.replace( /[" "|" "]*$/, "");
}
//左右空格;
function trim(s){
  return rtrim(ltrim(s));
}

The above is the detailed content of Detailed explanation of JS regular replacement method to remove spaces. 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