简体   繁体   English

从文本中删除所有空格

[英]Remove ALL white spaces from text

$("#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.在获得另一个 ID 的文本属性后,我想将 class 添加到一个 ID。 The problem with this, is the ID holding the text I need, contains gaps between the letters.问题在于,包含我需要的文本的 ID 包含字母之间的间隙。

I would like the white spaces removed.我想删除空格。 I have tried TRIM() and REPLACE() but this only partially works.我试过TRIM()REPLACE()但这只是部分有效。 The REPLACE() only removes the 1st space. REPLACE()仅删除第一个空格。

You have to tell replace() to repeat the regex:您必须告诉 replace() 重复正则表达式:

.replace(/ /g,'')

The g character makes it a "global" match, meaning it repeats the search through the entire string. g字符使其成为“全局”匹配,这意味着它在整个字符串中重复搜索。 Read about this, and other RegEx modifiers available in JavaScript here .此处阅读有关此内容以及 JavaScript 中可用的其他 RegEx 修饰符的信息。

If you want to match all whitespace, and not just the literal space character, use \s instead:如果要匹配所有空格,而不仅仅是文字空格字符,请改用\s

.replace(/\s/g,'')

You can also use .replaceAll if you're using a sufficiently recent version of JavaScript, but there's not really any reason to for your specific use case, since catching all whitespace requires a regex, and when using a regex with .replaceAll , it must be global, so you just end up with extra typing:如果您使用的是足够新的 JavaScript 版本,您也可以使用.replaceAll ,但是对于您的特定用例没有任何理由,因为捕获所有空格需要正则表达式,并且当使用带有.replaceAll的正则表达式时,它必须是全球性的,所以你最终会额外输入:

.replaceAll(/\s/g,'')
.replace(/\s+/, "") 

Will replace the first whitespace only , this includes spaces, tabs and new lines.仅替换第一个空格,这包括空格、制表符和新行。

To replace all whitespace in the string you need to use global mode要替换字符串中的所有空格,您需要使用全局模式

.replace(/\s/g, "")

Now you can use "replaceAll":现在您可以使用“replaceAll”:

console.log(' a b    c d e   f g   '.replaceAll(' ',''));

will print:将打印:

abcdefg

But not working in every possible browser:但不适用于所有可能的浏览器:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replaceAll https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replaceAll

Using String.prototype.replace with regex, as mentioned in the other answers, is certainly the best solution.如其他答案中所述,将String.prototype.replace与正则表达式一起使用当然是最好的解决方案。

But, just for fun, you can also remove all whitespaces from a text by using String.prototype.split and String.prototype.join :但是,为了好玩,您还可以使用String.prototype.splitString.prototype.join从文本中删除所有空格:

 const text = ' ab c defg '; const newText = text.split(/\s/).join(''); console.log(newText); // prints abcdefg

Regex for remove white space用于删除空格的正则表达式

\s+

 var str = "Visit Microsoft;". var res = str,replace(/\s+/g; ""). console;log(res);

or或者

[ ]+

 var str = "Visit Microsoft;". var res = str,replace(/[ ]+/g; ""). console;log(res);

Remove all white space at begin of string删除字符串开头的所有空格

^[ ]+

 var str = " Visit Microsoft;". var res = str,replace(/^[ ]+/g; ""). console;log(res);

remove all white space at end of string删除字符串末尾的所有空格

[ ]+$

 var str = "Visit Microsoft; ". var res = str,replace(/[ ]+$/g; ""). console;log(res);

let str = 'a big fat hen clock mouse '
console.log(str.split(' ').join(''))
// abigfathenclockmouse

Use replace(/\s+/g,'') ,使用replace(/\s+/g,'')

for example:例如:

const stripped = '    My String With A    Lot Whitespace  '.replace(/\s+/g, '')// 'MyStringWithALotWhitespace'

Using.replace(/\s+/g,'') works fine;使用.replace(/\s+/g,'') 工作正常;

Example:例子:

this.slug = removeAccent(this.slug).replace(/\s+/g,'');

Use replace(/ +/g,'_') :使用replace(/ +/g,'_')

Example:例子:


let text = "I Love you";
text.replace(/ +/g,'_');

simple solution could be: just replace white space ask key value简单的解决方案可能是:只需替换空格询问键值

val = val.replace(' ', '')
function RemoveAllSpaces(ToRemove)
{
    let str = new String(ToRemove);
    while(str.includes(" "))
    {
        str = str.replace(" ", "");
    }
    return str;
}

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

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