user357034
user357034

Reputation: 10981

need to strip out unwanted text

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

Upvotes: 1

Views: 99

Answers (1)

Mark Byers
Mark Byers

Reputation: 837946

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];

Upvotes: 2

Related Questions