简体   繁体   English

使用javascript删除空格

[英]Remove spaces using javascript

How to remove all spaces from string using javascript. 如何使用javascript从字符串中删除所有空格。 I have a code like this-- 我有这样的代码 -

var Title = "";
var Str="g g";
Title = Str.replace(/^\s+|\s+$/g, '');
this gives me result Title=g g
how to get result Title=gg

Use following regexp: 使用以下正则表达式:

Str.replace(/\s+/g, '');

The one you have right now just strippes spaces from start or end of your string. 你现在拥有的那个只是从你的字符串的开头或结尾剥离空格。

尝试这个

Title = Str.replace(/ /g, '');

Change code to: 将代码更改为:

var title = Str.replace(/\s+/g, '');

Your original regular expression would trim spaces only from the beginning or the end of the string. 您的原始正则表达式将仅从字符串的开头或结尾修剪空格。

You can just use: 你可以使用:

var Str="g g";
Str.replace(" ", "");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM