Regex to store a word before and after in javascript
This regex will get you the number just before of
:
"1 - 7 of 8 items".match(/\d+(?=\s+of\s+)/)
I am not expert at regex but I tried.
var findings = "1 - 8 of 8 items 1 - abc of ghi items".match(/\w+ of \w+/g);var collection = [];findings.forEach(function(item){ var words = item.split(' '); collection.push({first: words[0], second: words[2]});});console.log(collection);
You could split the string at non-digits:
var ar = "1 - 8 of 8 items".split(/[^\d]+/)
which gives
Array [ "1", "8", "8", "" ]
Now you can compare the 2nd and 3rd element of the array
ar[1] == ar[2]