正则表达式

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

\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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值