JavaScript正则表达式

    //1.new的方式创建表达式

    var regexp = new RegExp(/123/);

    console.log(regexp);

    //2.字面量的方式创建

    var rg = /123/;

    //3.regexp.test(str)判断str是否符合正则表达式对象

    console.log(/123/.test('123')); //true

    console.log(/123/.test('1234')); //true

    console.log(/123/.test('01234')); //true,这种是只要包含123就可以

    console.log(/123/.test('0134')); //false

    //4.^以什么开头 $以什么结尾

    console.log((/^123/).test('0123')); //false

    console.log((/123$/).test('0123')); //true

    console.log((/^abc$/).test('abcabc')); //false,这种限定只能是abc

    //5.[]代表只要包含其中一个就可以,

    console.log((/^[abc]$/).test('abc')); //false,只有a或者只有b或者只有c的时候才是true

    console.log((/^[abc]$/).test('aag')); //false,只有a或者只有b或者只有c的时候才是true

    console.log((/^[abc]$/).test('axx')); //false,只有a或者只有b或者只有c的时候才是true

    console.log((/^[abc]$/).test('b')); //true,只有a或者只有b或者只有c的时候才是true

    //6.-代表范围

    console.log((/^[a-z]$/).test('hjkh')); //false//代表只匹配一个a-z内的任一个

    console.log((/^[a-z]$/).test('h')); //true//代表只匹配一个a-z内的任一个 

    //7.字符组合,仅匹配一位数字或者字母或者下划线或者-

    console.log((/^[a-zA-Z0-9_-]$/).test('-S')); //false

    console.log((/^[a-zA-Z0-9_-]$/).test('-')); //true

    console.log((/^[a-zA-Z0-9_-]$/).test('9')); //true

    //8.中括号内有^表示取反,不能包括...

    console.log((/^[^a-zA-Z0-9_-]$/).test('-')); //false

    console.log((/^[^a-zA-Z0-9_-]$/).test('!!')); //false

    console.log((/^[^a-zA-Z0-9_-]$/).test('!')); //true

    //9.量词 *代表>=0次,+代表>=1次,?代表1||0,{3}代表3次,{3,}代表>=3,{3,6}代表>=3&&<=6次

    console.log(/^a*$/.test('')); //true

    console.log(/^a+$/.test('a')); //true

    console.log(/^a?$/.test('aa')); //false,出现1次或者不出现才为true

    console.log(/^a?$/.test('')); //true

    console.log(/^a{3}$/.test('aaa')); //true

    console.log(/^a{3}$/.test('aaaa')); //false

    console.log(/^abc{3}$/.test('aaaabccc')); //false,以ab开头 c重复3次

    console.log(/^abc{3}$/.test('abccc')); //true

    //10.案例,验证6-10位用户名是否合法

    console.log(/^[a-zA-Z0-9_-]{6,10}$/.test('andy')); //false

    console.log(/^[a-zA-Z0-9_-]{6,10}$/.test('jackson')); //true

    //11.预定义类,\d数字[0-9]、\D非数字[^0-9]、

    //\w任意字母数字和下划线相当于[A-Za-z0-9_]、\W相当于[^A-Za-z0-9_]、

    //\s匹配空格(换行符、制表符、空格)相当于[\t\r\n\v\f] , \S匹配非空格字符,相当于[^\t\r\n\v\f]

    //12.座机号码验证 123-12345678 1234-1234567

    var phonereg = /^\d{3,4}-\d{7,8}$/

    console.log(phonereg.test('123-12345678'));

    //13.replace(oldstr,newstr)函数

    console.log('andy和red'.replace('andy', 'blob')); //blob和red

    //14.正则表达式参数,g代表全局匹配,i忽略大小写,gi全局匹配和忽略大小写

    console.log('andy和red既激情又gay'.replace(/激情|gay/g, '**')); //andy和red既**又**

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

q124467623

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值