-1

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.

tv4free
  • 287
  • 3
  • 17
  • 8
    Show what you have tried so far please. – Jerry Sep 16 '13 at 18:27
  • 1
    This site was very helpful for me when I was learning regexes: http://www.regular-expressions.info/reference.html – Gary Sep 16 '13 at 18:28
  • I tried this [(Aries \()|\)] and it works...but the text Aries is dynamic – tv4free Sep 16 '13 at 18:29
  • @tv4free: Could you please edit your initial question to include the code that you have written thus far? – Conspicuous Compiler Sep 16 '13 at 18:33
  • Something along these lines? [How do you pass a variable to a regular expression](http://stackoverflow.com/questions/494035/how-do-you-pass-a-variable-to-a-regular-expression-javascript) – Gary Sep 16 '13 at 18:41
  • @tv4free, I assume you mean `str = "Aries (march--something)"`.. and you are wanting to update the `str` var? Do you want to match the word before the parens (Aries)? Can that word change to or do you only want to match "Aries" followed by a space, followed by stuff in parens? Can the string contain more than just this? Like "foo bar Aries (march--something) blah"? What is your expected output for whatever possible inputs? – Smern Sep 16 '13 at 19:14

2 Answers2

0

As long as there is no nesting of parenthesis following would work:

var str = 'Aries (march--something)';
var r = str.replace(/\([^)]*\)/g, '()');
//=> "Aries ()"
anubhava
  • 761,203
  • 64
  • 569
  • 643
0

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; });
fred02138
  • 3,323
  • 1
  • 14
  • 17