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);
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!