www.webdeveloper.com
Results 1 to 3 of 3

Thread: RegEx woes: Getting all submatches

  1. #1
    Join Date
    Sep 2011
    Posts
    2

    RegEx woes: Getting all submatches

    Hi,

    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.

  2. #2
    Join Date
    Aug 2007
    Posts
    3,767
    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.

  3. #3
    Join Date
    Sep 2011
    Posts
    2
    Quote Originally Posted by Declan1991 View Post
    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();
    	}

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles