views:

41

answers:

1

I need to strip out all text before and including "(" and all text after and including ")" in this variable.

var this_href = $(this).attr('href');

The above code produces this...

javascript:change_option('SELECT___100E___7',21);

However it can produce any text of any length within the parenthesis.

A Regex solution is OK.

So in this case I want to end up with this.

'SELECT___100E___7',21
+2  A: 

Rather than stripping out what you don't want you can match want you want to keep:

/\((.*?)\)/

Explanation:

\(    Match an opening parenthesis.
(     Start capturing group.
.*?   Match anything (non-greedy so that it finds the shortest match).
)     End capturing group.
\)    Match a closing parenthesis.

Use like this:

var result = s.match(/\((.*?)\)/)[1];
Mark Byers
Ok, but how to put this into something usable? How do I take the old variable and make a new one or replace the value in the variable with the sliced value? Not sure what to do with this.
thanks for the example