0

How can I get the reference directly after a matched word including the matched word in regex

Example

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]

Thank You for your help it is appreciated

EDIT

I'd like to be able to match all reference possibilities AKA example... Matt | Matt 5 | Matt 5:5 | Matt 5:5-10 | Matt 5:5-10, 12 | Matt 5:5-10, 12-14

EDIT

This is the Regex I came up with located here

I am trying to match all possible references

matt
matt 5
matt 5, 6, 7
matt 5:5
matt 5:5-10
matt 5:5-10, 16,
matt 5:5-10, 16-20, 18-20
matt 5-6

And according to the site I am, but when I paste the code into my page it still only comes up with the name.

The Regex is...

(matt( \d*(\:\d*)?(\-\d*)?((, (\d*\-)?\d*)?)+)?(?!\w))

what am I doing wrong?

Juan Gonzales
  • 1,967
  • 2
  • 22
  • 36
  • 1
    I would start with using the correct variable names, it's `pattern`, not `$pattern` ? – adeneo Jan 18 '14 at 00:55
  • +1 lol, this is just an example, not my actual code. Been between php and js all day. I'll edit. Thanks – Juan Gonzales Jan 18 '14 at 00:59
  • Using this Regex should match the scripture references completely if they follow the pattern you provided: (matt \d*:\d*) The parentheses constitute a capturing group so you should be able to easily access the result. See a topic on that here: http://stackoverflow.com/questions/432493/how-do-you-access-the-matched-groups-in-a-javascript-regex Also, see this regexr example: http://regexr.com?381t3 – Harvey A. Ramer Jan 18 '14 at 01:20
  • This looks promising @HarveyA.Ramer but do you have a regex that could catch all scripture possibilities? Matt - Matt 5 - or Matt 5:10 – Juan Gonzales Jan 18 '14 at 01:48
  • @JuanGonzales do you want to match all possible books of the Bible? I think this gets close: `(\w*(?= \d) \d*(:)?(\d*)?)` But to match all the books of the Bible without any numeric reference, you would need to pattern match based on an array of names, I think. There are other problems with this RegEx. It assumes that a number is a chapter reference, and there could be other numbers in your text. Probably starting with a list of the books to match against would be best. You can see it and test its shortcomings at http://regexr.com?381tu – Harvey A. Ramer Jan 18 '14 at 04:41
  • I am getting all the books by looping through an array of possible names – Juan Gonzales Jan 18 '14 at 04:56
  • Also, I did start a file on regexr and it all matched very well on the site, but when I put it inside my js it didn't work – Juan Gonzales Jan 18 '14 at 05:07

3 Answers3

1

I think this is what you want

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]
  • The document from Mozilla said that the function match returns array of string that match or return null is no string is matched.
  • It seem like you try to match string that string with matt and following by number:number. The regular expression to match this string should be matt \d+:\d+
invisal
  • 11,075
  • 4
  • 33
  • 54
  • This looks Awesome, but the only problem I have is that I have to get the name first. Im checking an object to see if the name exists within that object first, after I verify that then I need to add the digits behind the matched word. – Juan Gonzales Jan 18 '14 at 01:10
1

Here's a function that I use for this kind of regex matches. It takes a string, a regex with capturing groups, an a callback where you can access those groups. this is an object to assign matches, and return this outputs that object:

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']
// ]
elclanrs
  • 92,861
  • 21
  • 134
  • 171
0

Okay after days of working on it, I found my own solution... Thanks for the help...

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
Juan Gonzales
  • 1,967
  • 2
  • 22
  • 36