【问题标题】:How to remove string between specified characters and store the removed string (Javascript)如何删除指定字符之间的字符串并存储删除的字符串(Javascript)
【发布时间】:2018-01-28 17:59:13
【问题描述】:

假设我有一个字符串:

"but i [C#min] do [G#min] believe there's"

如何将该字符串转换为:

"but i  do  believe there's" (basically removing everything in-between '[' and ']')

我想将[C#min][G#min] 存储在另一个数组中。

【问题讨论】:

  • 你试过什么?您具体需要哪些帮助?
  • 不合逻辑
  • 使用正则表达式获取括号内的内容
  • 在不确切知道您要做什么的情况下,您可能可以使用stackoverflow.com/a/4292483/212869 这将返回一个没有括号之间文本的新字符串(经过适当编辑后)。存储删除的项目会删除它们的上下文。您还需要存储位置数据。

标签: javascript


【解决方案1】:

    var a = "but i [C#min] do [G#min] believe there's";
    console.log(a.replace(/\[(.[^\[\]]*)\]/g, '')); // "but i  do  believe there's"
    console.log(a.match(/\[(.[^\[\]]*)\]/g)); // ["[C#min]", "[G#min]"]

正则表达式匹配[] 之间的所有内容,[] 本身除外。

您可能需要删除替换周围留下的多余空格。

【讨论】:

    【解决方案2】:

    你可以使用正则表达式:

        var text = "but i [C#min] do [G#min] believe there's";
        var regexp = /(\[[^\]]*\])/g;
    
        var matches = text.match(regexp);
        text = text.replace(regexp, '').replace(/  /g, ' ');
        console.log(text);

    matches 将包含数组["[C#min]", "[G#min]"]

    注意:用于文本的第二个 replace 处理不正确的剩余双空格。

    【讨论】:

      【解决方案3】:

      您可以使用正则表达式查找括号中的字符串,然后使用String.replace() 将它们删除。剥离的部分将放在一个数组中,用于您需要的任何用途。

      var input = "but i [C#min] do [G#min] believe there's";
      var reg = /\[(.*?)\]/g;           // Patthen to find
      var matches = input.match(reg);   // Find matches and populate array with them
      var output = input.replace(reg, "").replace(/\s\s/g, " ");  // Strip out the matches and fix spacing
      console.log(matches); // Just for testing
      console.log(output);  // Result

      【讨论】:

        【解决方案4】:

        没有正则表达式,简单的方法:

            var string = "but i [C#min] do [G#min] believe there's";
            array = []; 
            filteredString = ""
            
            for (i=0;i<string.length;i++){
                
                if (string[i] != '[') {
                    filteredString += string[i];
                    continue;
                }
                newstring = ""+string[i];
                do {
                        newstring += string[++i]; 
                    
                } while (string[i]!=']');
                array.push(newstring);
            }
        
        console.log(array);
        console.log(filteredString);

        【讨论】:

          【解决方案5】:

          试试这个简单的方法:

              var str = "but i [C#min] do [G#min] believe there's";
              
              var start, stop, newStr = '', removed = '', counter =1;
              while(str.search(/\[.*\]/g) > 0) {
                start = str.indexOf('[');
                stop = str.indexOf(']');
                newStr += str.substring(0, start);
                removed += str.substring(start, stop+1);
                str =  str.substring(stop+1, str.length);
                counter++;
                if(counter > 3) break;
              }
              newStr += str;
              
              console.log(newStr);

          【讨论】:

            【解决方案6】:

            你可以用 [:

            分割字符串
            function parseString(input) {
                output = {
                    values: [],
                    result: ""
                };
                input = input.split('[');
                for (var index = 0; index < input.length; index++) {
                    var closingPosition = input[index].indexOf("]");
                    if ((index > 0) && (closingPosition + 1)) {
                        output.values.push('[' + input[index].substring(0, closingPosition) + ']');
                        if (input[index].length > closingPosition) {
                            output.result += input[index].substring(closingPosition + 1);
                        }
                    } else {
                        output.result += input[index];
                    }
                }
                return output;
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2019-01-11
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2015-08-04
              • 2016-07-01
              • 1970-01-01
              相关资源
              最近更新 更多