简体   繁体   English

如何删除指定字符之间的字符串并存储删除的字符串(Javascript)

[英]How to remove string between specified characters and store the removed string (Javascript)

Let's say I have a string: 假设我有一个字符串:

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

How to I turn that string into: 如何将字符串转换为:

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

And I would like to have [C#min] & [G#min] stored in an another array. 我想将[C#min][G#min]存储在另一个数组中。

  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]"] 

The RegExp matches for everything between [ , ] except for [ , ] themselves. RegExp匹配[]之间的所有内容,除了[]本身。

You might need to remove extra white spaces left around the replacements. 您可能需要删除替换项周围剩余的空白。

You may use a regular expression: 您可以使用正则表达式:

  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 will contain the array ["[C#min]", "[G#min]"] . matches将包含数组["[C#min]", "[G#min]"]

Note: the second replace for text handles the incorrectly remaining double spaces. 注意:第二个replace为text的字符将处理错误地保留的双精度空格。

Try this straightforward way: 试试这种简单的方法:

  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); 

Without Regex, simple way: 没有正则表达式,简单的方法是:

  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); 

You can use a regular expression to find the bracketed strings and then strip them out with String.replace() . 您可以使用正则表达式查找括号中的字符串,然后使用String.replace()它们。 The stripped out portions will be in an array for whatever use you need them for. 剥离的部分将按阵列排列,以供您使用。

 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 

You could split the string by [: 您可以将字符串分割为[:

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;
}

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

相关问题 如何使用jQuery或JavaScript中的索引删除字符串之间的字符? - how to Remove the characters in between a string using index in jquery or javascript? 删除 JavaScript 字符串中 2 个特定字符串之间的字符 - Remove characters between 2 specifics strings in a JavaScript string JavaScript 从两个字符之间的字符串中删除字符 - JavaScript remove characters from a string between two characters 如何使用javascript删除字符串中指定单词之后和句点之前的所有字符? - How can I remove all characters after a specified word and before a period in a string using javascript? 从字符串中删除字符并返回带有删除字符的新字符串 - Remove characters from string and return new string with removed characters 如何从javascript中的字符串末尾删除9个字符 - How to remove 9 characters from the end of the string in javascript 如何创建一个 function 以从具有起始索引和要删除的字符数的字符串中删除字符串 - How to create a function to remove a string from a string with a starting index and number of characters to be removed 不使用正则表达式 Javascript 删除两个字符之间的字符串 - Remove string between two characters using no regex Javascript 计算字符串中两个指定单词之间的字符 - Counting characters between 2 specified words in a string 识别字符串中已删除的字符 - Identifying removed characters in a string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM