How to get attribute inside square brackets with JavaScript

293 views Asked by At

I want to get the value of the background attribute (background.jpg) inside square bracket like BBCode editor's and I tried this regexp

\[(\w+)[^\]]*](.*?)\[\/\1]/g 

but it didn't work.

[code background="background.jpg"] Here some text [/code]
2

There are 2 answers

1
Avinash Raj On BEST ANSWER

Use the below regex and get the value of background attribute from group index 2.

\[(\w+)\s\S+?="([^\]"]*)"](.*?)\[\/\1]

DEMO

0
vks On
\[(\w+)[^\]]*?background="([^"]+)"[^\]]*\](.*?)\[\/\1]

Try this.See demo.

http://regex101.com/r/lZ5mN8/8