0

Let's say I have a string that looks like this:

var string = "(This is an auto-generated string) and this is useless text I don't need";

What I would like to do is assign another variable the string INSIDE the parentheses so the second variable would look like this:

var selected_string = "This is an auto-generated string";

How would I retrieve data around the parentheses and then assign it to a variable in JavaScript?

Thanks!

Harshul Sahni
  • 140
  • 1
  • 1
  • 10

1 Answers1

0

You can use a regular expression to pull out any information between parens:

var regex          = /\((.*?)\)/;
var matched        = regex.exec(string);
var selectedString = matched[1];