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 refrence我从哪里得到的

这是我到目前为止所做的demo

请帮忙

最佳答案

一个先行的方法可以解决此问题:

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

但在一般情况下,基于execreplace的循环会导致更简单的代码:
words = []
a.replace(/\[(.+?)\]/g, function($0, $1) { words.push($1) })

09-27 07:46