JavaScript正则表达式模式匹配(1)——基本字符匹配

时间:2021-11-15 17:06:07
 var pattern=/g..gle/;    //点符号表示匹配除了换行符外的任意字符
var str='g78gle';
alert(pattern.test(str)); var pattern=/go*gle/; //o* ,表示0个或者多个o
var str='goooooooooooogle';
alert(pattern.test(str)); var pattern=/go+gle/; //o+,表示一个或者多个o
var str='gogle';
alert(pattern.test(str)); var pattern=/go?gle/; //o?,表示0个或者1个o
var str='google';
alert(pattern.test(str)); var pattern=/go{2,4}gle/; //o{2,4},表示匹配o 2-4次,包括2和4
var str='google';
alert(pattern.test(str)); var pattern=/go{3}gle/; //o{2,4},表示匹配o只能3次
var str='google';
alert(pattern.test(str)); var pattern=/go{3,}gle/; //o{2,4},表示匹配o3次或3次以上
var str='google';
alert(pattern.test(str)); var pattern=/[a-z]oogle/; //[a-z]表示26个小写字母,任意一个都可以匹配
var str='google';
alert(pattern.test(str)); var pattern=/[0-9]*oogle/; //[0-9]*,表示0次一次或者多次
var str='4444444oogle';
alert(pattern.test(str)); var pattern=/[a-zA-Z0-9]oogle/; //[a-zA-Z0-9]表示大小写a-zA-z0-9
var str='9oogle';
alert(pattern.test(str)); var pattern=/[^0-9]oogle/; //[^0-9]表示非0-9的任意字符
var str='_oogle';
alert(pattern.test(str)); var pattern=/^[0-9]oogle/; //这个^符号,是加在/后面的而不是[]里面的 表示首字符匹配
var str='1oogle';
alert(pattern.test(str)); var pattern=/^[0-9]+oogle/; //这个^符号,是加在/后面的而不是[]里面的 +表示可以匹配一个或者多个
var str='11111oogle';
alert(pattern.test(str)); var pattern=/\woogle/; // \w表示匹配任意字母数字及下划线
var str='woogle';
alert(pattern.test(str)); var pattern=/\Woogle/; // \W表示匹配非任意字母数字及下划线
var str='woogle';
alert(pattern.test(str)); var pattern=/\doogle/; // \d 表示[0-9]
var str='2oogle';
alert(pattern.test(str)); var pattern=/\Doogle/; // \D 表示非[0-9]
var str='aoogle';
alert(pattern.test(str)); var pattern=/^[a-z]oogl[0-9]$/; // ^强制首匹配 $强制尾匹配
var str='aoogle';
alert(pattern.test(str)); var pattern=/goo\sgle/; // \s表示空格匹配
var str='goo gle';
alert(pattern.test(str)); var pattern=/google\b/; // \b表示是否到达边界
var str='googled';
alert(pattern.test(str)); var pattern=/google|baidu|bing/; // |表示是否匹配或选择模式
var str='baidu';
alert(pattern.test(str));