{xxx}、[xxx]、/xxx/ 这些统称字面量,即:在代码中声明对象的一种简写方式。{xxx} ==> new Object() [xxx] ==> new Array() /xxx/ ==> new RegExp()字面量也可以按json的解析方式获得对象。 var p="\\d"; alert("a123".match(eval("(/" + p + "/)")));--- String.match返回的结果就是一个string数组。 var a=("123-8test888-9hehe".match(/(\d{3})\-(\d)/g)); alert(typeof a[0]); // string$1、$2不是RegExp的prototype的属性,而是RegExp的直接属性(RegExp为构造函数)。 var a=("123-8test888-9hehe".match(/(\d{3})\-(\d)/g)); alert(RegExp.$1);
这些统称字面量,即:在代码中声明对象的一种简写方式。{xxx} ==> new Object()
[xxx] ==> new Array()
/xxx/ ==> new RegExp()字面量也可以按json的解析方式获得对象。
var p="\\d";
alert("a123".match(eval("(/" + p + "/)")));---
String.match返回的结果就是一个string数组。
var a=("123-8test888-9hehe".match(/(\d{3})\-(\d)/g));
alert(typeof a[0]); // string$1、$2不是RegExp的prototype的属性,而是RegExp的直接属性(RegExp为构造函数)。
var a=("123-8test888-9hehe".match(/(\d{3})\-(\d)/g));
alert(RegExp.$1);
上面用alert(RegExp.$1);返回的是123,如果要返回下一个匹配的呢?也就是如何再继续得到888呢
matchs(0)表示主匹配,像上面的就是123-8
matchs(0).SubMatches(0)表示子匹配,要访问第几个子匹配就在SubMatches后面写,这样用两个for循环就可以打印出所有的值了,js里我是纠结了很长时间啊。
var match = str.match(/(\d{3})\-(\d)/g, str);
for(var i = 0; i < match.length; i++) {
alert(match[i]);
}
RegExp.$1和RegExp.$2也只能访问第一个匹配的结果啊,没有办法访问所有的吗?
var str = "123-8test888-9hehe";
var re = /(\d{3})\-(\d)/g;
var match;
while ((match = re.exec(str)) != null)
alert([RegExp.$1, RegExp.$2]);
RegExp.$1是有个有意思的玩意,可有可无,要是它可以再加1维就完美了。
while (match = re.exec(str))//这样写也可以的吧
--
RegExp.$2
RegExp.$2返回的是8,不是888,你可以测试下,RegExp只保存了第一个匹配结果。
火狐扩展FillForm的更新数据文件部分需要用变量构造成正则表达式来replace,而你的回答正好解决了这一难题,您的id已经被添加入贡献者列表。