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

How to use regular expressions in JS to replace multiple spaces in a string with one space

php中世界最好的语言
Release: 2018-03-30 11:02:51
Original
5069 people have browsed it

This time I will show you how to use regular expressions in JS to replace multiple spaces in a string with one space. How to use regular expressions in JS to replace multiple spaces in a string with one space. NotesWhat are the following? Let’s take a look at the actual cases.

javascriptReducing multiple spaces in a string to one space code example:In practical applications, there may be such a need, that is, multiple spaces in a string Spaces are reduced to one space.
The following is a code example that can achieve this function.
The code is as follows:

//对字符串扩展
String.prototype.ResetBlank=function(){
 var regEx = /\s+/g; 
 return this.replace(regEx, ' '); 
};
var str="jb51.net is a good  site";
console.log(str.length);
console.log(str.ResetBlank().length);
Copy after login
The above code realizes our requirements. For more information, please refer to related readings.

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Detailed analysis of matching single characters with regular expressions

How to use regular expressions to verify the address function of IPV4

The above is the detailed content of How to use regular expressions in JS to replace multiple spaces in a string with one space. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!