Home >Web Front-end >JS Tutorial >A daily javascript learning summary (RegExp object)_javascript skills
1、正则表达式test方法
var text = "cat, bat, sat, fat"; var pattern = /.at/; if (pattern.test(text)){ alert("The pattern was matched."); }
2、正则的toString()方法
var pattern = new RegExp("\\[bc\\]at", "gi"); alert(pattern.toString()); // /\[bc\]at/gi alert(pattern.toLocaleString()); // /\[bc\]at/gi
3、RegExp Constructor(构造函数) Properties(属性)
var text = "this has been a short summer"; var pattern = /(.)hort/g; /* * Note: Opera doesn't support input, lastMatch, lastParen, or multiline. * Internet Explorer doesn't support multiline. */ if (pattern.test(text)){ alert(RegExp.input); //this has been a short summer alert(RegExp.leftContext); //this has been a alert(RegExp.rightContext); // summer alert(RegExp.lastMatch); //short alert(RegExp.lastParen); //s alert(RegExp.multiline); //false } input 保存被搜索的字符串 index 保存匹配的首字符的位置 lastIndex 保存匹配的字符串下一个字符的位置 lastMatch 保存匹配到的字符串 lastParen 保存最后一个被匹配的字符串(最后一个括号内的内容) leftContext 保存匹配字符串左边的内容 rightContext 保存匹配字符串右边的内容 $1~$9 保存最开始的9个子匹配(括号中的内容)
var text = "this has been a short summer"; var pattern = /(.)hort/g; /* * Note: Opera doesn't support short property names. * Internet Explorer doesn't support multiline. */ if (pattern.test(text)){ alert(RegExp.$_); //this has been a short summer alert(RegExp["$`"]); //this has been a alert(RegExp["$'"]); // summer alert(RegExp["$&"]); //short alert(RegExp["$+"]); //s alert(RegExp["$*"]); //false } * 分为长属性名和短属性名 * input $_ 最近一次要匹配的字符串 * lastMatch $& 最近一次的匹配项 * lastParen $+ 最近一次匹配的捕获组 * leftContext $` input字符串中lastMatch之前的文本 * multiline $* 布尔值,表示是否所有表达式都使用多行模式。 * rightContext $' input字符串中lastMatch之后的文本
4、正则$1.....$9
var text = "this has been a short summer"; var pattern = /(..)or(.)/g; if (pattern.test(text)){ alert(RegExp.$1); //sh alert(RegExp.$2); //t } 每当产生一个带括号的成功匹配时,$1...$9 属性的值就被修改。 可以在一个正则表达式模式中指定任意多个带括号的子匹配,但只能存储最新的九个。
5、RegExp exec()
var text = "mom and dad and baby"; var pattern = /mom( and dad( and baby)?)?/gi; var matches = pattern.exec(text); alert(matches.index); //0 第一个被匹配到的位置 alert(matches.input); //"mom and dad and baby" 匹配的原始字符串 alert(matches[0]); //"mom and dad and baby" 匹配的第一个值 alert(matches[1]); //" and dad and baby" 匹配的第二个值 alert(matches[2]); //" and baby" 匹配的第三个值
var text = "cat, bat, sat, fat"; var pattern1 = /.at/; var matches = pattern1.exec(text); alert(matches.index); //0 alert(matches[0]); //"cat" alert(pattern1.lastIndex);//0 matches = pattern1.exec(text); alert(matches.index); //0 alert(matches[0]); //"cat" alert(pattern1.lastIndex);//0 var pattern2 = /.at/g; var matches = pattern2.exec(text); alert(matches.index); //0 alert(matches[0]); //"cat" alert(pattern2.lastIndex);//0 matches = pattern2.exec(text); alert(matches.index); //5 alert(matches[0]); //"bat" alert(pattern2.lastIndex);//0
6、RegExp实例属性
var pattern1 = /\[bc\]at/i; alert(pattern1.global); //false //是否设置全局查找 alert(pattern1.ignoreCase); //true 是否忽略大小写 alert(pattern1.multiline); //false 是否设置多行查找 alert(pattern1.lastIndex); //0 一个整数,标示开始下一次匹配的字符位置。 alert(pattern1.source); //"\[bc\]at" 正则表达式的源文本。 var pattern2 = new RegExp("\\[bc\\]at", "i"); alert(pattern2.global); //false alert(pattern2.ignoreCase); //true alert(pattern2.multiline); //false alert(pattern2.lastIndex); //0 alert(pattern2.source); //"\[bc\]at"
以上就是今天的javascript学习小结,之后每天还会继续更新,希望大家继续关注。