views:

23

answers:

2

how do i extract the part between the brackets in javascript regular expressions:

answer_postal_address[address]

I tried this:

/^\w*/g

which works but i wasn't sure if this was correct...

+1  A: 

Matches input of value of any word character followed by anything inside of bracket, i.e. answer_postal_address[address]

  var re = new RegExp("\w+\[([^\]]+)\]");
  var m = re.exec(input);
  if (m == null) {
    alert("No match");
  }
  else {
    alert("Matched: " + m[1]);
  }
Jason McCreary
A: 

Shouldn't this be sufficient?

Search for: \([.*?]\)

Replace with: $1

So

result = str.replace(/\((.*?)\)/g, "$1");
Vantomex
Oops, I misread your sample, apparently you used square-brackets. I have corrected (edited) the regex a bit.
Vantomex