...

View Full Version : Regex Help



bszen27
09-02-2011, 07:33 PM
I am creating a userscript and trying to take the following:

Arian Foster (Hou - RB)

and end up just with the team name:

Hou

You can't remove by index (which I would be able to do), because it might be K instead of RB or NO instead of Hou. I am assuming that you use Regex and the replace function to do so, but I am struggling with understanding all Regex symbols. Can someone help me with this and explain it briefly? I would appreciate it.

venegal
09-03-2011, 12:16 PM
var string = 'Arian Foster (Hou - RB)';
var matches = string.match(/(.*?) \((.*?) - (.*?)\)/);


matches[0] will be the whole string,
matches[1] will be "Arian Foster",
matches[2] will be "Hou",
matches[3] will be "RB"

Brief explanation:
The / in the beginning and the end delimits the regexp (like " does for strings); everything you put between parens withing the regexp will be available as an entry in the resulting matches array; . matches any character, * matches any number of whatever came before, so .* matches any number of any character; ? tells * to not be greedy, meaning it will match as few characters as possible, which is important if you have several of the same patterns in your string (here, it's optional, but it's important you know how it works); the parens in your string have to be escaped in the regexp (using \), because, as already mentioned, unescaped parens are interpreted as partial match delimiters within regexps, and not as literal parens.

I think that's all I used.

Also, I moved your thread to the Javascript forum, since it has nothing to do with the DOM or with JSON.

desiter
09-03-2011, 12:36 PM
So the solution is:

var fullString = "Arian Foster (Hou - RB)";
var teamName = fullString.replace(/\w+\s\w+\s\((\w+)\s\-\s\w+\)/, "$1");
alert(teamName);

Explanation of the replace pattern:
\w - any word character (letter)
\w+ - at least one word character
\s - white space character
\( - just left bracket (escaped by \)
\- - just a dash (escaped by \)
\) - just left bracket (escaped by \)

Explanation of the replacement pattern:
$1 - a phraze matching the first pattern in brackets which is (\w+) (your team name pattern)

To summarize, the replacement pattern matches the whole full user string and replace() method replaces it by a phraze matching first pattern in brackets

Arbitrator
09-05-2011, 07:14 AM
I am creating a userscript and trying to take the following:

Arian Foster (Hou - RB)

and end up just with the team name:

Hou

...

Can someone help me with this and explain it briefly? I would appreciate it.This can be done very simply using what is apparently called a "capturing group":


var team_name = ("Arian Foster (Hou - RB)").match(/\(([a-z]+)/i)[1];
document.defaultView.alert(team_name);

The parentheses capture part of a match (i.e., [a-z]+) and return those parts as additional elements in a match array. In the above case, the first element in the match array is the whole match (i.e., "(Hou") and the second element (indicated by [1]) in that array contains the part that you want to capture (i.e., "Hou").

I found http://stackoverflow.com/questions/3003310/how-can-i-match-on-but-exclude-a-regex-pattern/3004046 and http://www.regular-expressions.info/brackets.html to be informative on the subject.

If you're trying to take the whole string and replace it with just the team name, desiter's code shows you how to do that using what is called a "backreference" (i.e., $1). Those are explained in the second of the two aforementioned resources.

bszen27
09-09-2011, 01:46 PM
Thank you for the great explanations!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum