如何在匹配的单词(包括正则表达式中的匹配词)之后直接获得引用
示例
var refArray = [];
var str = "matt 25:5 hello foo bar matt 5:10"; // unknown information
var pattern = "matt"; // looped through an object to get pattern
var regex = new RegExp(pattern,"gi");
var matches = str.match(regex); // check for any name matches
if(matches){
/*
This is where I get stuck
Get the numbers right after each match
and place both name and number into a variable AKA. result
*/
refArray.push($result);
}
// The refArray should output [matt 25:5, matt 5:10]谢谢你的帮助,非常感谢
编辑
我想能匹配所有的参考可能性AKA的例子..。马特5:5马特5:5马特5:5马特马特5:5-10马特5:5-10
编辑
这是我想出的定位这里的Regex
我正在努力匹配所有可能的参考资料
哑光 马特5 马特5,6,7 马特5:5 马特5:5-10 马特5:5-10,16, 马特5:5-10,16-20,18-20 马特5-6
根据我的站点,但是当我将代码粘贴到我的页面时,它仍然只会给出名称。
霸王龙是..。
(matt( \d*(\:\d*)?(\-\d*)?((, (\d*\-)?\d*)?)+)?(?!\w))我做错什么了?
发布于 2014-01-21 01:41:49
经过几天的努力,我找到了自己的解决方案.谢谢你的帮助..。
var pattern = '([0-9][ ]|[0-9]?)([a-z]+)([.]?[ ]?)((([0-9]{1,3})(:[0-9{1,3}](-[0-9]{1,3})*)*([-, ?])*)+)';
var rxref = new RegExp(pattern, "gi");
var refArray = str.match(rxref); // get the match with digits and put them inside an array发布于 2014-01-18 00:59:51
我想这就是你想要的
var refArray = [];
var str = "matt 25:5 hello foo bar matt 5:10";
var pattern = "matt \\d+:\\d+";
var regex = new RegExp(pattern,"gi");
refArray = str.match(regex);
alert(refArray);
// The result is [matt 25:5, matt 5:10]match返回匹配或返回null的字符串数组,不匹配字符串。matt进行匹配,并随后由number:number进行匹配。匹配此字符串的正则表达式应该是matt \d+:\d+。发布于 2014-01-18 01:13:46
下面是用于这种正则匹配的函数。它需要一个字符串,一个带有捕获组的正则表达式,一个可以访问这些组的回调。this是一个指定匹配的对象,return this输出该对象:
function matchAll(str, regex, fn) {
var result = [];
str.replace(regex, function() {
var args = [].slice.call(arguments, 1);
result.push(fn.apply([], args));
});
return result;
}
var str = 'matt 25:5 hello foo bar matt 5:10';
var name = 'matt';
var regex = RegExp('('+ name +') (\\d+:\\d+)', 'gi');
var result = matchAll(str, regex, function(name, number) {
this.push(name, number);
return this;
});
//^
// [
// ['matt', '25:5']
// ['matt', '5:10']
// ]https://stackoverflow.com/questions/21198657
复制相似问题