I'm trying to build a regex that will match BBCode attributes. I want it to match attributes without quotation marks (attr=value) and also with them (attr="value"), but don't match if there's only one mark (attr="value or attr=value").
I managed to get close to a solution with this regex /\[size\=((?=")"(.*?)"|([^"]*))\](.*?)\[\/size\]/is (for size tags), using a lookahead, but it places the match for the quoted and unquoted attributes on different indexes. Here's a scheme:
+-----------------------+----------+----------+----------+----------+
| Input | Match #1 | Match #2 | Match #3 | Match #4 |
+-----------------------+----------+----------+----------+----------+
| [size="7"]txt[/size] | "7" | 7 | | txt |
+-----------------------+----------+----------+----------+----------+
| [size=7]txt[/size] | 7 | | 7 | txt |
+-----------------------+----------+----------+----------+----------+
While this is not a major issue, I'd like to know if there's any wya to match them to the same index.
Background Info:
Basically, I'm working on improving a BBCode parser. As for now, it only allows attributes to be placed without quotation marks ". Now, that brings a whole lot of limitations to it, so I'm adding support to quoted attributes. Problem is I can't break backwards compatibility, so I gotta come up with something that works for both cases.
Answer
Since the group which matches your first case must be distinct from the group matching the second case, I don't believe there's any way to get them to match to same index/group.
It's not unusual to see code working around this like the following though:
attr = matches[1] or matches[2]
Since you can know for sure that at most 1 of those elements are non-empty, the or logic works.
No comments:
Post a Comment