<?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>