如何使用JavaScript從字符串中刪除空格?

[英]How to remove spaces from a string using JavaScript?


How to remove spaces in a string? For instance:

如何刪除字符串中的空格?例如:

Input : '/var/www/site/Brand new document.docx'
Output : '/var/www/site/Brandnewdocument.docx'

輸入:'/var/www/site/全新的文檔。多克斯的輸出:/ var / www /網站/ Brandnewdocument.docx”

Thanks

謝謝

6 个解决方案

#1


813  

This?

這個嗎?

str = str.replace(/\s/g, '');

Example

例子

var str = '/var/www/site/Brand new document.docx';

document.write( str.replace(/\s/g, '') );


Update: Based on this question, this:

更新:基於這個問題,如下:

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

is a better solution. It produces the same result, but it does it faster.

是一個更好的解決方案。它產生同樣的結果,但它做得更快。

The Regex

正則表達式

\s is the regex for "whitespace", and g is the "global" flag, meaning match ALL \s (whitespaces).

\s是“空格”的正則表達式,g是“全局”標志,意思是匹配所有的\s(空格)。

A great explanation for + can be found here.

關於+的一個很好的解釋可以在這里找到。

As a side note, you could replace the content between the single quotes to anything you want, so you can replace whitespace with any other string.

作為補充說明,您可以將單引號之間的內容替換為您想要的任何內容,因此可以用任何其他字符串替換空白。

#2


41  

var a = b = " /var/www/site/Brand new   document.docx ";

console.log( a.split(' ').join('') );
console.log( b.replace( /\s/g, '') ); 

Two ways of doing this!

有兩種方法!

#3


11  

Following @rsplak answer: actually, using split/join way is faster than using regexp. See the performance test case

根據@rsplak的回答:實際上,使用split/join方法比使用regexp要快。請參見性能測試用例

So

所以

var result = text.split(' ').join('')

var =結果文本。(' '). join()

operates faster than

運行速度比

var result = text.replace(/\s+/g, '')

var =結果文本。替換(/ \ s + / g,”)

On small texts this is not relevant, but for cases when time is important, e.g. in text analisers, especially when interacting with users, that is important.

對於小文本,這是無關的,但是對於時間很重要的情況,例如在文本分析程序中,尤其是與用戶交互時,這是很重要的。


On the other hand, \s+ handles wider variety of space characters. Among with \n and \t, it also matches \u00a0 character, and that is what   is turned in, when getting text using textDomNode.nodeValue.

另一方面,\s+處理更廣泛的空間字符。在\n和\t中,它還匹配\u00a0字符,這就是為什么當使用textDomNode.nodeValue獲取文本時,將被打開。

So I think that conclusion in here can be made as follows: if you only need to replace spaces ' ', use split/join. If there can be different symbols of symbol class - use replace(/\s+/g, '')

所以我認為這里的結論可以是這樣的:如果你只需要替換空格',使用split/join。如果符號類可以有不同的符號-使用替換(/\s+/g)

#4


7  

var input = '/var/www/site/Brand new document.docx';

//remove space
input = input.replace(/\s/g, '');

//make string lower
input = input.toLowerCase();

alert(input);

Click here for working example

點擊這里查看工作示例

#5


5  

You could try using this:

你可以試試這個:

input.split(' ').join('');

#6


2  

  var output = '/var/www/site/Brand new document.docx'.replace(/ /g, ""); 
    or
  var output = '/var/www/site/Brand new document.docx'.replace(/ /gi,"");

Note: Though you use 'g' or 'gi' for removing spaces both behaves the same.

注意:雖然您使用'g'或'gi'來刪除空格,但它們的行為是相同的。

If we use 'g' in the replace function, it will check for the exact match. but if we use 'gi', it ignores the case sensitivity.

如果我們在replace函數中使用'g',它將檢查是否匹配。但是如果我們使用gi,它會忽略大小寫敏感性。

for reference click here.

點擊這里供參考。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2011/05/11/6f5455263ad889fa059188f18d4b819c.html



 
  © 2014-2022 ITdaan.com 联系我们: