匹配单引号或双引号之间的内容
源字符串:
<div class="ts-background-image ts-animate ts-scale" data-bg-image="assets/img/bg/11.png"></div><div class="ts-background-image ts-animate ts-scale" data-bg-image='assets/img/bg/22.jpg'></div><form class="ts-form ts-form-email" data-php-path="assets/php/email.php"></form>
正则一:
([\"\'])([^"\.]*\.[^"]{2,4})\1
PHP代码;
preg_match_all('/([\"\'])([^"\.]*\.[^"]{2,4})\1/', $txt, $matches);
var_dump($matches);die;
匹配结果:
array(3) {
[0]=>
array(3) {
[0]=>
string(22) ""assets/img/bg/11.png""
[1]=>
string(22) "'assets/img/bg/22.jpg'"
[2]=>
string(22) ""assets/php/email.php""
}
[1]=>
array(3) {
[0]=>
string(1) """
[1]=>
string(1) "'"
[2]=>
string(1) """
}
[2]=>
array(3) {
[0]=>
string(20) "assets/img/bg/11.png"
[1]=>
string(20) "assets/img/bg/22.jpg"
[2]=>
string(20) "assets/php/email.php"
}
}
正则二(限定了指定的后缀):
([\"\'])([^\"\.]*\.(png|jpg|svg|gif|jpeg|html))\1
匹配结果:
array(4) {
[0]=>
array(2) {
[0]=>
string(22) ""assets/img/bg/11.png""
[1]=>
string(22) "'assets/img/bg/22.jpg'"
}
[1]=>
array(2) {
[0]=>
string(1) """
[1]=>
string(1) "'"
}
[2]=>
array(2) {
[0]=>
string(20) "assets/img/bg/11.png"
[1]=>
string(20) "assets/img/bg/22.jpg"
}
[3]=>
array(2) {
[0]=>
string(3) "png"
[1]=>
string(3) "jpg"
}
}
匹配空行
^(\s*)$\n
正则表达式测试工具
RegexBuddy
在线测试工具
https://regex101.com/