match方法
var str = "iid0000ffr";
var substr = str.match(/id(\S*)ff/);
console.log(substr)
返回结果为:["id0000ff", "0000"]
()里的\S*表达式匹配所有字符串
在高级语言里,我们会用一个叫数量词的概念:
(?=ff)这表示以ff结尾的前面的字符串,但不包括ff
var str = "iid0000ffr";
var substr = str.match(/(\S*)(?=ff)/);
console.log(substr)
返回数组:["iid0000", "iid0000"]
(iid=?)这表示以iid开头的字符串,但不包括iid
var str = "iid0000ffr";
var substr = str.match(/(iid=?)(\S*)/);
console.log(substr);
返回数组:["iid0000ffr", "iid", "0000ffr"]
跟第一个示例一样获取前后中间的字符串
var str = "iid0000ffr";
var substr = str.match(/(iid=?)(\S*)(?=ffr)/);
console.log(substr);
返回数组:["iid0000", "iid", "0000"]