Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex get all content between two characters [closed]

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

like image 487
Paul Adam Harrison Avatar asked Nov 07 '13 13:11

Paul Adam Harrison


People also ask

What does \+ mean in regex?

To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \. matches "." ; regex \+ matches "+" ; and regex \( matches "(" .

What is \r and \n in regex?

Matches a form-feed character. \n. Matches a newline character. \r. Matches a carriage return character.

How do you match a space in regex?

\s stands for “whitespace character”. Again, which characters this actually includes, depends on the regex flavor. In all flavors discussed in this tutorial, it includes [ \t\r\n\f]. That is: \s matches a space, a tab, a carriage return, a line feed, or a form feed.

How do you use square brackets in regex?

Synopsis. Use square brackets ( [] ) to create a matching list that will match on any one of the characters in the list. Virtually all regular expression metacharacters lose their special meaning and are treated as regular characters when used within square brackets.


2 Answers

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();
like image 91
3 revs Avatar answered Oct 12 '22 14:10

3 revs


How about

yourStringVariable.match(/\[(.*)\]/)[1]
like image 21
OGHaza Avatar answered Oct 12 '22 14:10

OGHaza