Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
6
rated 0 times [  7] [ 1]  / answers: 1 / hits: 23348  / 12 Years ago, wed, june 13, 2012, 12:00:00

Possible Duplicate:

Regular Expression to find a string included between two characters, while EXCLUDING the delimiters




I have a function where I have to get text which is enclosed in square brackets but not brackets for example


this is [test] line i [want] text [inside] square [brackets]

from the above line I want words:


test
want
inside
brackets

I am trying with to do this with /[(.*?)]/g but I am not getting satisfied result, I get the words inside brackets but also brackets which are not what I want


I did search for some similar type of question on SO but none of those solution work properly for me here is one what found (?<=[)[^]]+(?=]) this works in RegEx coach but not with JavaScript. Here is reference from where I got this


here is what I have done so far: demo


please help.


More From » jquery

 Answers
120

A single lookahead should do the trick here:



 a = this is [test] line i [want] text [inside] square [brackets]
words = a.match(/[^[]]+(?=])/g)


but in a general case, exec or replace-based loops lead to simpler code:



words = []
a.replace(/[(.+?)]/g, function($0, $1) { words.push($1) })

[#84944] Tuesday, June 12, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
deonkalvinw

Total Points: 409
Total Questions: 96
Total Answers: 89

Location: Saint Pierre and Miquelon
Member since Sun, Nov 27, 2022
2 Years ago
deonkalvinw questions
Sun, Feb 6, 22, 00:00, 2 Years ago
Tue, Dec 28, 21, 00:00, 2 Years ago
Sun, Aug 22, 21, 00:00, 3 Years ago
Sun, Mar 7, 21, 00:00, 3 Years ago
;