Remove ALL white spaces from text

Possible Duplicate:
Replace all spaces in a string with '+'

$("#topNav" + $("#breadCrumb2nd").text().replace(" ", "")).addClass("current");

This is a snippet from my code. I want to add a class to an ID after getting another ID's text property. The problem with this, is the ID holding the text I need, contains gaps between the letters.

I would like the white spaces removed. I have tried TRIM() and REPLACE() but this only partially works. The REPLACE() only removes the 1st space.


You have to tell replace() to repeat the regex:

.replace(/ /g,'')

The g character means to repeat the search through the entire string. Read about this, and other RegEx modifiers available in JavaScript here.

If you want to match all whitespace, and not just the literal space character, use s as well:

.replace(/s/g,'')

.replace(/s+/, "") 

should work (Regex that removes all spaces)

or you can try this

.replace(/s/g, "") 

(globally replace spaces)

链接地址: http://www.djcxy.com/p/4726.html

上一篇: 在javascript中删除字符串内的空格

下一篇: 从文本中删除所有空格