简体   繁体   English

如何使用JavaScript在方括号内获取属性

[英]How to get attribute inside square brackets with JavaScript

I want to get the value of the background attribute (background.jpg) inside square bracket like BBCode editor's and I tried this regexp 我想要像BBCode编辑器一样在方括号内获取background属性(background.jpg)的值,我尝试了此正则表达式

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

but it didn't work. 但这没用。

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

Use the below regex and get the value of background attribute from group index 2. 使用下面的正则表达式从组索引2中获取background属性的值。

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

DEMO DEMO

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

Try this.See demo. 试试看。看演示。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM