分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 技术分享

js正则表达式

发布时间:2023-09-06 01:21责任编辑:白小东关键词:js正则

语法

/pattern/attributes ??????????????/正则表达式主体/修饰符(可选)
new RegExp(pattern, attributes);

修饰符

表达式

元字符

量词

RegExp对象正则表达式方法

正则表达式.RegExp方法(字符串)

 test() 方法用于检测一个字符串是否匹配某个模式,如果字符串中含有匹配的文本,则返回 true,否则返回 false。

var patt = /^\d+$/; ???//一个到多个数字并且以数字开头以数字结尾patt.test("1203abc"); ?????//判断这个字符串是否为[1,n]个位数字.结果为false

  var patt = /\d+/      //[1,n]位的数字
  patt.test("1203abc");  //检测中存在[1,n]数字,这里结果为true

exec() 方法检索字符串中的指定值。返回值是被找到的值。如果没有发现匹配,则返回 null。

var patt = /^\d+$/; ??//[1,n]位数字的正则表达式,并且以数字开头以数字结尾patt.exec("1203abc456"); ???//字符串匹配匹配正则表达式时返回,否则返回null.这里返回nullvar patt = /\d+/; ???//[1,n]位数字的正则表达式patt.exec("1203abc456"); ???////匹配字符串存在这个字符串

 compile() 方法也可用于改变和重新编译正则表达式。

var str="Every man in the world! Every woman on earth!";patt=/man/g;str2=str.replace(patt,"person");console.info(str2);patt=/(wo)?man/g;patt.compile(patt);str2=str.replace(patt,"person");console.info(str2);

支持正则表达式的 String 对象的方法

字符串.方法(正则表达式)

js正则表达式

原文地址:http://www.cnblogs.com/aeolian/p/7767385.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved