John
John

Reputation: 11

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.

Upvotes: 0

Views: 63

Answers (6)

Lajos Arpad
Lajos Arpad

Reputation: 77063

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

Upvotes: 0

ild flue
ild flue

Reputation: 1371

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

Upvotes: 0

francisaugusto
francisaugusto

Reputation: 1197

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

Upvotes: 0

Scott Marcus
Scott Marcus

Reputation: 65873

You can use a regular expression to find the bracketed strings and then strip them out with 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

Upvotes: 0

gargsms
gargsms

Reputation: 818

    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.

You might need to remove extra white spaces left around the replacements.

Upvotes: 2

Devilish Alchemist
Devilish Alchemist

Reputation: 11

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

Note: the second replace for text handles the incorrectly remaining double spaces.

Upvotes: 1

Related Questions