【问题标题】:Remove ALL white spaces from text从文本中删除所有空格
【发布时间】:2011-10-01 04:21:26
【问题描述】:
$("#topNav" + $("#breadCrumb2nd").text().replace(" ", "")).addClass("current");

这是我的代码中的一个 sn-p。在获得另一个 ID 的文本属性后,我想向一个 ID 添加一个类。问题在于,包含我需要的文本的 ID 包含字母之间的间隙。

我想删除空格。我试过TRIM()REPLACE(),但这只是部分有效。 REPLACE() 只删除第一个空格。

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:
    .replace(/\s+/, "") 
    

    将替换仅第一个空格,这包括空格、制表符和换行符。

    要替换字符串中的所有空格,您需要使用全局模式

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

    【讨论】:

      【解决方案2】:

      你必须告诉 replace() 重复正则表达式:

      .replace(/ /g,'')
      

      g 字符使其成为“全局”匹配,这意味着它在整个字符串中重复搜索。阅读此内容以及 JavaScript here 中可用的其他 RegEx 修饰符。

      如果您想匹配所有空格,而不仅仅是文字空格字符,请改用\s

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

      如果您使用的是最新版本的 JavaScript,您也可以使用 .replaceAll,但对于您的特定用例没有任何理由,因为捕获 all 空格需要正则表达式,并且当使用带有.replaceAll 的正则表达式时,它必须是全局的,所以你最终会额外输入:

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

      【讨论】:

      • Coffeescript 会出于某种可怕的原因抱怨该正则表达式。相反,我不得不继续使用.replace(/\s+/g, ''),这对我来说完全没问题。
      【解决方案3】:

      如其他答案所述,将String.prototype.replace 与正则表达式一起使用无疑是最佳解决方案。

      但是,为了好玩,您还可以使用 String.prototype.splitString.prototype.join 删除文本中的所有空格:

      const text = ' a b    c d e   f g   ';
      const newText = text.split(/\s/).join('');
      
      console.log(newText); // prints abcdefg

      【讨论】:

        【解决方案4】:

        删除空格的正则表达式

        \s+
        

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

        [ ]+
        

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

        删除字符串开头的所有空格

        ^[ ]+
        

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

        删除字符串末尾的所有空格

        [ ]+$
        

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

        【讨论】:

          【解决方案5】:

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

          例如:

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

          【讨论】:

            【解决方案6】:

            使用 .replace(/\s+/g,'') 可以正常工作;

            例子:

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

            【讨论】:

              【解决方案7】:

              现在你可以使用“replaceAll”了:

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

              将打印:

              abcdefg
              

              但并非在所有可能的浏览器中都可以使用:

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

              【讨论】:

              • 我还注意到replaceAll 方法的运行速度几乎是正则表达式解决方案的两倍。除非您处理的数量非常大,否则这并不重要,但很高兴知道。
              【解决方案8】:
              function RemoveAllSpaces(ToRemove)
              {
                  let str = new String(ToRemove);
                  while(str.includes(" "))
                  {
                      str = str.replace(" ", "");
                  }
                  return str;
              }
              

              【讨论】:

                【解决方案9】:

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

                例子:

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

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 2011-11-29
                  • 2014-06-19
                  • 2012-04-14
                  • 2018-01-10
                  • 2018-03-11
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多