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

Detailed explanation of JS regular replacement method to remove spaces

黄舟
Release: 2017-03-25 14:45:48
Original
1446 people have browsed it

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));
}
Copy after login

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));
}
Copy after login

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!

Related labels:
js
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template