开发者

One regex for tag with multiple parameters and replacement

开发者 https://www.devze.com 2023-03-09 05:25 出处:网络
[tag]foobar[/tag] [tag parameter1]foobar[/tag] [tag parameter2=\"whatever\"]foobar[/tag] [tag parameter1 parameter2=\"whatever\"]foobar[/tag]
[tag]foobar[/tag]
[tag parameter1]foobar[/tag]
[tag parameter2="whatever"]foobar[/tag]
[tag parameter1 parameter2="whatever"]foobar[/tag]
[tag parameter2="whatever" parameter1]foobar[/tag]

Can I match all these combinations in one expression? Ideally suitable for additional parameters. I tried a few things but nothing works. For now i am going to match every single combination but i guess, that's not the best.

The second matter, i want to make a replacement for the parameters.

parameter1 to class="tagclass"

parameter2="input" to id="in开发者_运维知识库put"

Is it possible to do it all in one step?

Thanks and regards, Alex


WordPress has code that does that:

http://core.trac.wordpress.org/browser/trunk/wp-includes/shortcodes.php

It doesn't work 100%, but it gets the job done. See in particular the shortcode_parse_atts() function (line 225 as I write, but subject to change in later versions of WP).

0

精彩评论

暂无评论...
验证码 换一张
取 消