I'm trying to extract one or multiple usernames from a given search term:
cartoons -user:donald,mickey,goofy
However, with the regex I came up with I only get two submatches:
Code:
/\-user\:(\w+)(?:\,(\w+))*/i
submatches: 1: donald 2: goofy
I've been twiddling with this for two hours now, but still don't know what I'm doing wrong. How do I have to change the expression to get all usernames as submatches?
Thanks in advance.
As far as I know, there's no way to do it like that. You could use this, even if it's a bit of a workaround. You may have to tweak it however, depending on how the file is organised.
Code:
var arr = str.replace(/.*\-user\:(\w+)((\,\w+)*).*/i,"$1$2").split(",");
Great wit and madness are near allied, and fine a line their bounds divide.
As far as I know, there's no way to do it like that [...]
Code:
var arr = str.replace(/.*\-user\:(\w+)((\,\w+)*).*/i,"$1$2").split(",");
Yeah. After further googling I stumbled upon the term "atomic groups", which seems to be what I'm used to from other languages but isn't supported in js :~( So I solved my problem with String.split as you suggested. Thanks for your help!
Code:
/**
* Parse the user input for search parameters and store them in yt.searchParams.
* Known parameters are:
* -user:<user-1>,<user-2>,...,<user-20> limit search to videos uploaded by specific users
* @param {String} searchTerm User input containing the search term plus optional parameters
* @return {String} The search term without parameters
*/
yt.parseSearchTerm = function(searchTerm) {
var users = [];
function getSubmatches() {
users = getSubmatches.arguments[1].split(",");
return "";
}
searchTerm = searchTerm.replace(yt.searchParams.expr.USER_PARAM, getSubmatches);
yt.searchParams.users = users;
//!DEBUG:
for (var i = 0, field; field = yt.searchParams.users[i]; ++i) println("#" + i + ": " + field);
return searchTerm.trim();
}
Bookmarks