I need a regular expression to match anything that is within <p>
tags so for example if I had some text:
<p>Hello world</p>
The regex would match the Hello world part
In regular expressions, we can match any character using period "." character. To match multiple characters or a given set of characters, we should use character classes.
[] denotes a character class. () denotes a capturing group. [a-z0-9] -- One character that is in the range of a-z OR 0-9.
Basically (0+1)* mathes any sequence of ones and zeroes. So, in your example (0+1)*1(0+1)* should match any sequence that has 1. It would not match 000 , but it would match 010 , 1 , 111 etc. (0+1) means 0 OR 1.
in javascript:
var str = "<p>Hello world</p>";
str.search(/<\s*p[^>]*>([^<]*)<\s*\/\s*p\s*>/)
in php:
$str = "<p>Hello world</p>";
preg_match_all("/<\s*p[^>]*>([^<]*)<\s*\/\s*p\s*>/", $str);
These will match something as complex as this
< p style= "font-weight: bold;" >Hello world < / p >
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With