如果我有多個空格的字符串單詞之間:使用JavaScript /正則表達式,如何刪除多餘的內部空間,使之成爲在JavaScript /正則表達式中,如何刪除字符串中的雙空格?
Be an excellent person
:
Be an excellent person
如果我有多個空格的字符串單詞之間:使用JavaScript /正則表達式,如何刪除多餘的內部空間,使之成爲在JavaScript /正則表達式中,如何刪除字符串中的雙空格?
Be an excellent person
:
Be an excellent person
像這樣的東西應該能去做吧。
var text = 'Be an excellent person';
alert(text.replace(/\s\s+/g, ' '));
此正則表達式應該解決的問題:
var t = 'Be an excellent person';
t.replace(/ {2,}/g, ' ');
// Output: "Be an excellent person"
您可以使用正則表達式/\s{2,}/g
:
var s = "Be an excellent person"
s.replace(/\s{2,}/g, ' ');
注意:如果您試圖從(例如)製表符分隔的文件中刪除多個空格,則有時會刪除製表符。這是答案和伊江答案之間的唯一區別。 – Annan 2012-11-22 18:58:50
他們似乎都工作,所以我投了起來。請注意,我在測試字符串中添加了前導空格和尾部空格。 http://jsfiddle.net/karim79/YhG3h/2/ – karim79 2010-12-17 02:28:14
http://stackoverflow.com/questions/1981349/regex-to-replace-multiple-spaces-with-a-single-space – Recep 2010-12-29 13:58:34
我希望你把你的妻子*放在一個基座上 – 2016-09-16 02:14:04