Using JavaScript RegEx.
How can I match a <p>
element(including attributes), but not <param>
or other HTML elements starting with a "P".
Using JavaScript RegEx.
How can I match a <p>
element(including attributes), but not <param>
or other HTML elements starting with a "P".
Try:
/(<p(?:\s+[^>]*)?>)/i
/
( #start capture group
<p #match '<p'
(?: #start non-capture group
\s+ #match one or more white space characters
[^>]* #match zero or more characters that arent >
)? #end non-capture group - make it optional
> #match '>'
) #end capture group
/i #end regexp - make case insensitive
/<(?:p|P)\s+/.exec(s);
Although it doesn't match the entire tag, but that's quite complicated considering that the tag closing symbol > is allowed inside an attribute.
<(p|P)([\s].*)?>
seems to work good =). But you shouldn't use RegEx when you can use DOM, or even XML/XPath/whatever.
/<p\b[^>]*>/i
\b
matches a word boundary; coming after the 'p' it means the next character (if there is a next character) is not a letter, digit or underscore.
Disclosure: [^>]*
isn't really the correct way to match the rest of the tag, since attribute values can legally contain angle brackets. But it's probably good enough, and that's not what the question is about anyway.