0

I have a string that may be: Hello (hiii(1998 overhill) hpp) there. I want to strip to remove the brackets and content from within but keeping the words Hello there.

Below is my code but it removes the first part: (hiii(1998 overhill) leaving hpp) behind.

myString = "Hello (hiii(1998 overhill) hpp) there";
myString = myString.replace(/ \([^)]*\)/g, '');

With that said, i want it to do this for all occurrences which is why i supplied the /g but how do i do it for the last occurrence of (content) hi) also? For future reference.

So if i have "Hello (ddd(dog) kk) and hello (pineapple(g)" is it possible to cut out the last bracket so im left with: Hello (ddd(dog) kk) and hello removing the (pineapple(g)?

Thanks!

William
  • 809
  • 11
  • 31
  • 2
    From `Hello (hiii(1998 overhill) hpp) there` you expect `Hello there` and from `Hello (ddd(dog) kk) and hello (pineapple(g)` you expect `Hello (ddd(dog) kk) and hello`? – Marco Nov 07 '16 at 15:44
  • Exactly!:) Possible? – William Nov 07 '16 at 15:45
  • 1
    I can't see a pattern here to work on that regex – Marco Nov 07 '16 at 15:46
  • How about leaving the last part. Is it possible to just make `Hello (hiii(1998 overhill) hpp) there` work? Or do you have an idea as to how i could accomplish it? – William Nov 07 '16 at 15:49
  • With this https://regex101.com/r/uwCyzi/1 you can remove everything between the brackets. – Marco Nov 07 '16 at 15:52

5 Answers5

1

Using simple regex may not be suitable to deal with nested structures. Try recursion or regex-recursion instead as shown in the two first answers for this similar question.

Community
  • 1
  • 1
Marcos
  • 36
  • 1
  • 4
1

Just split the string into an array based on spaces, and add elements at zero position and last position:

myString = myString.split(" ");
var result = myString[0] + " " + myString[myString.length-1];
Akash
  • 71
  • 1
  • 11
1

Here is a non-regex version of code that will work on all examples given:

var newStr = str.split(' ').map(function(sec) {
    if (sec[0] !== '(' && sec[sec.length-1] !== ')' ) {
    return sec
  }
}).join(' ').replace(/\s\s+/g, ' ')

However, it will not work if there is a word surrounded by white spaces in the original string. That means for the string: Hello (hiii(1998 Teddybear overhill) hpp) there it would return Hello Teddybear there.

Since you did not specify that, the code will take care of all the supplied cases.

Falk
  • 577
  • 4
  • 11
  • From what I understood `Hello (hiii(1998 Teddybear overhill) hpp) there` should also return `Hello there` – Marco Nov 07 '16 at 16:11
1

Not sure if I understood the question correctly but you seem to want to remove everything within the braces also if there is an uneven amount of braces. This is probably an overkill but here is my attempt

JSbin example

myString = "Hello (hiii(1998 overhill) hpp) there";

function noBraces(str){

  let arr = str.split('')
      openBrace = null,
      closeBrace = null;

  // find first opening brace
  for (let i = 0, l = arr.length; i < l; i++){
    if (arr[i] == "(" ) {
      openBrace = i;
      break
    }
  }

  // find last closing brace
  for (let i = 0, l = arr.length - 1; i <= l; l--){  
    if (arr[l] == ")") {
      // we don't need the brace in the result
      closeBrace = l + 1;
      break
    }
  }

  // check if both braces exist and return result
  if (openBrace != null && closeBrace != null){
    return str.slice(0,openBrace) + str.slice(closeBrace, str.length);
  }
  // if only one brace exists then return everything
  // on one side of the brace
  else if (openBrace != null && closeBrace == null){
    return str.slice(0,openBrace);
  } else if (openBrace == null && closeBrace != null){
    return str.slice(closeBrace,str.length);
  } else {
    // if no braces found return the string
    return str;
  }

}

console.log(noBraces(myString));
AntK
  • 928
  • 8
  • 21
1

I found two ways of possible situations you want.

One of them is the one that I told you in the comment:

var myString = 'Hello (ddd(dog) kk) and hello (pineapple(g)';
console.log(myString.replace(/\(.+\)/g, ''));

This one will remove from the first ( to the last ).


Or removing pairs of brackets contents recursively:

function removeBracketsContents(str) {
  while (str.indexOf('(') > -1 & str.indexOf(')') > -1) {
    str = str.replace(/\([^\(\)]+\)/g, '');
  }
  return str;
}

console.log(removeBracketsContents('Hello (hiii(1998 overhill) hpp) there'));
console.log(removeBracketsContents('Hello (ddd(dog) kk) and hello (pineapple(g))'));
Marco
  • 844
  • 1
  • 12
  • 25