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
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 "(" .
Matches a form-feed character. \n. Matches a newline character. \r. Matches a carriage return character.
\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.
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.
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();
How about
yourStringVariable.match(/\[(.*)\]/)[1]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With