String needs match everything except content inside paranthesis
str = Aries (march--something)
i want to strip Aries(dynamic text) and space and brackets.
I tried :
/([^\(]*)?\(([^\)]*)?\)/
didn't work.
String needs match everything except content inside paranthesis
str = Aries (march--something)
i want to strip Aries(dynamic text) and space and brackets.
I tried :
/([^\(]*)?\(([^\)]*)?\)/
didn't work.
As long as there is no nesting of parenthesis following would work:
var str = 'Aries (march--something)';
var r = str.replace(/\([^)]*\)/g, '()');
//=> "Aries ()"
You're attempting to match a pattern that excludes an internal substring, but javascript's regular expression implementation doesn't really support backreferences, which is how that would be done in a more powerful language.
The standard way to address this javascript is to use the replace method of the string object, which accepts a second parameter which is a function. This function is called with the results of the match, enabling you to recombine the matching groups as you require.
Here is the code for your case:
var str = "Aries (march--something)";
var result = str.replace(/([^\s]+)\s*(\()[^\)]*(\))/, function(match, g1, g2, g3) { return g1+g2+g3; });