Reputation: 109
I'm trying to achieve this "exampleexample" from this "example(unwanted thing)example", so task is to remove everything inside the parentheses as well as the parentheses. Trying to acheve this using loops, with current code I'm getting "example)example", thanks in advance!
const removeParentheses = function(s){
let arr = s.split("");
for(let i=0;i<arr.length;i++){
if(arr[i] === "("){
while(arr[i] !== ")"){
arr.splice(i, 1);
}
}
}
return arr.join("");
}
Upvotes: 0
Views: 91
Reputation: 370729
If there's no nesting, I'd use a regular expression instead - match (
, followed by non-)
,s followed by a )
:
const removeParentheses = s => s.replace(/\([^)]*\)/g, '');
\(
- literal (
[^)]*
- zero or more non-)
s\)
- literal )
Upvotes: 1