正则表达式

来源:互联网 发布:消防大数据内容 编辑:程序博客网 时间:2024/04/30 13:56

沈逸老师:实战级正则表达式一点通

\w 匹配 字母 数字 下划线\s 匹配任意`**一个**`空格.  除\r\n之外的所有字符^  从头开始$  结束符 () 对字符进行分组并保存匹配的本文。与位于小括号之间的模式匹配的内容都会被捕获js中:修饰符g表示global

练习一:匹配大小写及中文

<?php/** * Created by PhpStorm. * User: sxinboss * Date: 16-6-3 * Time: 下午8:53 */    $str='Hello World hello  WORLD HELLO 你  world好 Hello  world Hello  WORLD Hello';    //preg_match_all('/W(?i)orld/',$str,$res); //匹配大小写    preg_match_all('/[\x{4e00}-\x{9fa5}]/u',$str,$res);//匹配所有中文   修饰符u 表示utf8    print_r($res);?>
<script>    var str='Hello Worll World Wrold hello  WORLD HELLO  world Hello World  world Hello  WORLD Hello';    //var regx=/W[orld]+/g; //匹配大小写    var regx=/[\u4e00-\u9fa5]/g;//匹配所有中文    var res = str.match(regx);    alert(res);</script>

练习2:类似微博的@某某人

取出来的结果带@<?php    $str='12je0idsjf@SxinBoss Good Morning';    $str2='@SxinBoss Good Morning\';';    //preg_match_all('/@\w+\s*/',$str,$res);    preg_match_all('/^@\w+\s/',$str2,$res );//如果只能在开头才能@    print_r($res);?><script>    var str='12je0idsjf@SxinBoss Good Morning';    var str2='@SxinBoss Good Morning\';';    var regx=/@\w+\s/g;    var regx2=/^@\w+\s/g;//如果只能在开头才能@    alert(str.match(regx));    alert(str2.match(regx2));</script>

练习3:代码编辑器匹配到;

<?php    $str='alert("hello");';    preg_match_all('/^.+;$/',$str,$res );    print_r($res);?><script>    var str='alert("hello");';    var regx=/^.+;$/g;    alert(str.match(regx));</script>
0 0