I have an array of variables all containing text. I want to select only the items containing a minute-second marker, eg. “3:00”, “4:01”, “0:23”. I understand how to go through the array, I just need to know how to implement this regex.
Just to be safe, here it is:
^(([0-9])|([0-1][0-9])|([2][0-3])):(([0-9])|([0-5][0-9]))$
I’m trying to implement it in this:
$.each(comments, function(i, val) { //Go through array with jQuery
//Remove from array if instance of "minute:second" is not found
});
Thanks!
EDIT:
Dave Thomas requested examples. They are all YouTube comments. Examples of a comment with the minute:time and then one without are below. All taken from an example video.
- “1:00 Fifa 12 comentator?”
- “ROOOOOOOONEY <3”
EDIT2:
for (var i = 0, len = comments.length; i < len; i++) {
if(!(comments[i].test(/(([0-5][0-9])|[0-9])\:[0-9]{2,2}/))){
comments.splice(i,1);
}
}
You’ll want to be using the native filter function for fitlering arrays (there is a shim for older browsers in the link) instead of each, and you can test a regexp against a string using test: