Home > Web Front-end > JS Tutorial > body text

How to replace spaces with javascript

藏色散人
Release: 2023-01-05 16:11:45
Original
7023 people have browsed it

How to replace spaces in JavaScript: 1. Use "name.replace(" ","");" to replace; 2. Use "replace(new RegExp(/( )/g),""); "; 3. Use "name.split(" ").join("");" and so on.

How to replace spaces with javascript

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

Replacing spaces in the input box in JS is a very common operation when processing form requirements to prevent data anomalies caused by user operating habits and ensure the security of parameter transfer.

NO.1

name.replace(" ","");
Copy after login

The above method is a very simple replacement, but it has two weaknesses:

1. It can only replace a single English space or Chinese space (full-width) ;

2. Only the first match of the current string can be replaced.

NO.2

name.replace(new RegExp(/( )/g),"");
Copy after login

The above method uses regular matching and can replace all, but there is still a weakness:

1. Only English spaces or Chinese spaces can be replaced (full-width).

NO.3

name.split(" ").join("");
Copy after login

The above method is to separate and merge by characters, which can replace all, but there is still a weakness:

1. It can only replace English spaces or One of Chinese spaces (full width).

NO.4

name.replace(/(^\s*)|(\s*$)/g,"");
Copy after login

The above method uses regular matching and can replace English or Chinese spaces, but there is a weakness:

1. It can only replace the leading and trailing spaces. Does not work on spaces in the middle of the string.

Ultimate Killing Move

name.replace(/\s+/g,"");
Copy after login

The above method is through regular matching, which can replace English or Chinese spaces and replace them all.

[Note] There is no so-called replaceAll method in JS. The author's test result is "undefined" and cannot be recognized on the page. Of course, there is also a workaround, which is to rewrite the prototype of the replaceAll method based on the function of replace:

String.prototype.replaceAll = function(reallyDo, replaceWith, ignoreCase) { 
    if (!RegExp.prototype.isPrototypeOf(reallyDo)) { 
        return this.replace(new RegExp(reallyDo, (ignoreCase ? "gi": "g")), replaceWith); 
    } else { 
        return this.replace(reallyDo, replaceWith); 
    } 
}
Copy after login

[Recommended learning: javascript advanced tutorial]

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

Related labels:
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