0

I'm using javascript and I'm trying to write a regular expression that matches anything except left and right parenthesis and anything between them. After doing a little research, I came up with a similar example here on Stackoverflow. In this particular example the following regular expression was used to match any three digit except 999.

[(?!999)(\d{3})][1]  

This regular expression work as intended and matches 555 or 444 or 333 but not 999.

I want my regular expression to match as follow:

John Smith                     =>  John Smith
John Smith (RET)               =>  John Smith
Mr. John Smith's (RET) (1st)   =>  Mr. John Smith's
John William Smith (RET)       =>  John William Smith
John                           =>  John

This is what I have so far, but it's not working:

(?!\([A-Za-z]?\))(.*)

Can someone tell me where I've gone wrong.

Mutuelinvestor
  • 3,384
  • 10
  • 44
  • 75

3 Answers3

2

A more-obvious (to me, anyway) method would be to use the String.replace method with the Regex:

"John Smith (RET) (1st) SomethingElse".replace(/\([^\)]*\)/g, '');

This does not cover nested parentheses, however, in case that is at issue. In that case, this answer suggests using XRegExp to add the recursive parameter found in Perl-compatible regular expressions.

Community
  • 1
  • 1
Andrew
  • 14,325
  • 4
  • 43
  • 64
1

Is this what you're looking for:

[^\(\)]*

I've added an illustration with an input which turns green with valid matches and red with invalid matches

input:focus:invalid {
 background:red;
}
input:focus:valid {
 background:green;
}
<input  class="pattern" pattern = "[^\(\)]*" />
Cedric Ipkiss
  • 5,662
  • 2
  • 43
  • 72
  • Thanks for the reply, but I'm not sure I fully understand it. I tried /[^\(\)]*/ on regex 101 and found that it doesn't have any capturing groups do doesn't return anything even though it has matches and 2 that it matches things in between the ( ). – Mutuelinvestor Jan 04 '15 at 00:19
  • @Mutuelinvestor FWIW, you need to wrap that whole regex in `()` to get capture groups, and use the global modifier, though it's probably moot since it matches everything inside the parens. – Ryan J Jan 04 '15 at 00:23
  • @RyanJ I tried here on regex101 (https://regex101.com/r/xM8wU8/1) as you can see, its not yielding the results I was seeking. – Mutuelinvestor Jan 04 '15 at 00:31
  • @Mutuelinvestor yes, that's why I said FWIW (for what it's worth) and that it was probably moot (means it won't make a difference). It was for your information only. – Ryan J Jan 04 '15 at 00:40
0
[^)](?![^(]*\))

Try this.See demo.

https://regex101.com/r/wZ0iA3/1

vks
  • 67,027
  • 10
  • 91
  • 124