【问题标题】:Multiple Regex on String字符串上的多个正则表达式
【发布时间】:2011-02-17 18:06:00
【问题描述】:

如何将多个正则表达式应用于单个字符串?

例如,用户在文本区域中输入以下内容:

red bird
blue cat
black dog

我想用逗号替换每个回车符,用下划线替换每个空格,这样最后的字符串就变成了 red_bird,blue_cat,black_dog。

到目前为止,我已经尝试了以下语法的变化:

function formatTextArea() {
    var textString = document.getElementById('userinput').value;

    var formatText = textString.replace(
        new RegExp( "\\n", "g" ),",",
        new RegExp( "\\s", "g"),"_");

    alert(formatText);
}

【问题讨论】:

    标签: javascript regex replace


    【解决方案1】:

    您可以链接替换。 replace 方法的每个应用程序都会返回一个字符串,因此您可以在该字符串上再次应用替换。喜欢:

    function formatTextArea() {
        var textString = document.getElementById('userinput').value;
    
        var formatText = 
             textString.replace(/\n/g,",")
                       .replace(/\s/g,"_");
    
        alert(formatText);
    }
    

    顺便说一句,不需要所有这些新的正则表达式对象。使用正则表达式文字(如上面的/\n/g)。

    或者,您可以使用 lambda 进行替换

    const str = `red bird
    blue cat
    black dog`;
    console.log(str.replace(/[\n\s]/g, a => /\n/.test(a) ? "," : "_"));

    【讨论】:

      【解决方案2】:

      正如其他人所提到的,对于像您所要求的那样简单的事情,链接已经足够了。但是,如果您希望它更具动态性,您可以使用替换函数作为第二个参数:

      function formatTextArea() {
          var textString = document.getElementById('userinput').value;
      
          var formatText = textString.replace(/\n|\s/g, function ($0) {
              if ($0 === "\n")
                  return ",";
              else if ($0 === " ")
                  return "_";
          }
          alert(formatText);
      }
      

      使用替换函数可以让您保持动态,而无需将对replace() 的调用链接在一起。它也可能稍微快一点(正则表达式解析器只调用一次)。请注意,\s 将不仅仅匹配空格字符:-) 就您的问题而言,这已经足够了:

      var formatText = textString.replace(/\n|\s/g, function ($0) {
          return $0 == "\n" ? "," : "_";
      }
      

      【讨论】:

        【解决方案3】:

        包括http://phpjs.org/functions/str_replace:527,然后

        input = str_replace("\\n", ',', input);
        input = str_replace(' ', '_', input); 
        

        【讨论】:

        • 为什么要使用this? JavaScript 有一个原生的replace 函数。
        • 很容易看到懂一门语言、热爱它并低估他人的人。
        【解决方案4】:
        var textString = "red blue\nhack bye\ntest rt\n";
        var formatText = textString.replace(new RegExp( "\\n", "g" ),",").replace(new RegExp( "\\s", "g"),"_");
        alert(formatText);
        

        【讨论】:

          【解决方案5】:
          formatText = textString.replace(/\n/g,',').replace(/\s/g,'_');
          

          【讨论】:

            【解决方案6】:

            Regexp 对象有自己的文字符号,使用正斜杠,这意味着不必转义反斜杠。例如,/\n/g 等价于 new RegExp('\\n','g')

            function formatTextArea()
            {
                var textString = document.getElementById('userinput').value;
            
                textString = textString.replace(/\n/g,",").replace(/\s/g,"_");
            
                alert(textString);
            }
            

            【讨论】:

              猜你喜欢
              • 2015-01-11
              • 2019-03-03
              • 2013-04-26
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2011-02-19
              • 1970-01-01
              相关资源
              最近更新 更多