Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: Regex Help

  1. #1
    New Coder
    Join Date
    Oct 2009
    Posts
    30
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Regex Help

    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.

  • #2
    Gütkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    Code:
    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.

  • Users who have thanked venegal for this post:

    bszen27 (09-28-2011)

  • #3
    New to the CF scene
    Join Date
    Aug 2011
    Location
    Poland
    Posts
    8
    Thanks
    0
    Thanked 2 Times in 2 Posts
    So the solution is:
    Code:
    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
    Last edited by desiter; 09-03-2011 at 12:43 PM.
    Descript.org - lightweight JavaScript foundation framework

  • Users who have thanked desiter for this post:

    bszen27 (09-28-2011)

  • #4
    Senior Coder Arbitrator's Avatar
    Join Date
    Mar 2006
    Location
    Splendora, Texas, United States of America
    Posts
    3,300
    Thanks
    28
    Thanked 275 Times in 269 Posts
    Quote Originally Posted by bszen27 View Post
    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":

    Code:
    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/3...attern/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.
    Last edited by Arbitrator; 09-05-2011 at 07:18 AM. Reason: I fixed a typo.
    For every complex problem, there is an answer that is clear, simple, and wrong.

  • Users who have thanked Arbitrator for this post:

    bszen27 (09-28-2011)

  • #5
    New Coder
    Join Date
    Oct 2009
    Posts
    30
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Thank you for the great explanations!


  •  

    Posting Permissions

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