23

I need to get content between symbols [ and ] even there are other same characters i need to take content between first [ and last ]. In jquery useing regex. Thanks Advance

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
Paul Adam Harrison
  • 243
  • 1
  • 2
  • 8

2 Answers2

42

No need for jQuery, use standard Javascript :

var extract = str.match(/\[(.*)\]/).pop();

If your delimiters are { and }, change it to

var extract = str.match(/{(.*)}/).pop();
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • It works thanks and could you please write same think for symbols { and }, becouse there are some of same characters and i cant understand which one i need to change to get content from inside other characters. And please put a link of good regex tutorial. – Paul Adam Harrison Nov 07 '13 at 13:11
  • @Tafari What do you mean precisely ? – Denys Séguret Nov 07 '13 at 13:11
  • thanks. and how to detect if string starts for example [button ??? – Paul Adam Harrison Nov 07 '13 at 13:19
  • 2
    @PaulAdamHarrison (.*) basically says "capture as many characters as possible". if you want to capture {} instead of [], then replace [ with { and ] with }. a good regular expressions tutorial (and completely free) is http://www.regular-expressions.info/ – Nzall Nov 07 '13 at 13:21
  • @dystroy I mean doesn't it also capture the brackets? As he wanted only the content **between symbols [ and ]** – Tafari Nov 07 '13 at 14:58
  • @Tafari Read again : *"between first [ and last ]"*. – Denys Séguret Nov 07 '13 at 15:02
  • @dystroy and when it comes to your example: `bla[this should[be]included]not this`, you capture `[this should[be]included]`, which is not **BETWEEN** but rather _"text **FROM** first [ to last ]"_. Just to say I'm not using JQuery maybe thats the difference, as I don't really understand the `/` in your regex. – Tafari Nov 07 '13 at 15:30
  • @Tafari Open the console of your browser (probably F12) and paste `"bla[this should[be]included]not this".match(/\[(.*)\]/).pop()` – Denys Séguret Nov 07 '13 at 15:35
  • @Tafari What you might be missing is that there's a capturing group. – Denys Séguret Nov 07 '13 at 15:36
  • No I'm using [gskinner.com](http://gskinner.com/RegExr/) with the mentioned example and your pattern, and first it **removes** the `forward slashes` then it captures the string I mentioned. Well could you tell me what is the purpose of '/' in your regex? – Tafari Nov 07 '13 at 15:46
  • 1
    @Tafari I suggest you read some tutorial on JavaScript regular expressions, for example [this one](https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions). This is a JavaScript question, not a gskinner one. – Denys Séguret Nov 07 '13 at 15:48
  • 1
    @dystroy I put your regex into `JavaScript editing tool` and it works... Well I usually don't work with JavaScript that's way I was bit surprised that it captures the string correctly, as for example C# it would fail. Anyway thanks for the link and answers +1. – Tafari Nov 07 '13 at 15:52
  • what if i have multiple? like this • i want to get everything between < and > – moeiscool Oct 01 '16 at 19:20
1

How about

yourStringVariable.match(/\[(.*)\]/)[1]
OGHaza
  • 4,795
  • 7
  • 23
  • 29