#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    27
    Rep Power
    0

    Using a Regex to shorten values


    Hello,

    I need your help.

    The function that I have below will contain multiple matches, so for this example, I have significantly shortened it.

    My question for the Regex folks on here is, is there a way to shorten the if list of matches using some sort of special Regex?

    Here are some matches that the function below is checking for:

    BRIEFING NOTE TO BOSS1
    BRIEFING NOTE TO BOSS2
    BRIEFING NOTE TO BOSS3
    KEY MESSAGES
    MEDIA ADVISORY
    MEDIA CALL

    Here's my sample coding:

    Code:
    function shorten_name(val)
        if (val.match("BRIEFING")) { return "BN" }
        else if (val.match("KEY MESSAGES")) { return "KM" }
        else if (val.match("MEDIA ADVISORY")) { return "MA" }
        else if (val.match("MEDIA CALL")) { return "MC" }
    }
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,122
    Rep Power
    9398
    Not if you need special handling (ie, different return values) for each.

    I mean, you could match on "BRIEFING NOTE" and then pull the first letter from each matched word, but IMO that's just overcomplicating the issue and what you have now is best.
  4. #3
  5. CSS & JS/DOM Adept
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jul 2004
    Location
    USA (verifiably)
    Posts
    20,127
    Rep Power
    4304
    I think a better way to shorten code like that would be to use an associative array (which in JavaScript is called an "object").
    Spreading knowledge, one newbie at a time.

    Check out my blog. | Learn CSS. | PHP includes | X/HTML Validator | CSS validator | Common CSS Mistakes | Common JS Mistakes

    Remember people spend most of their time on other people's sites (so don't violate web design conventions).

IMN logo majestic logo threadwatch logo seochat tools logo