SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Wizard creole's Avatar
    Join Date
    Oct 2000
    Location
    Nashvegas Baby!
    Posts
    7,845
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Tricky regex: Allow only first occurence of a character

    I've got a tricky piece of regex I need help with.

    I have a string(s):
    used inventory/ford/focus/lx
    used-inventory/ford/focus
    aboutus/ourteam/andy matthews

    All of which need to have non alpha-numeric characters removed (barring dashes, and ONE slash, the first one). Given that, the above strings would need to be converted to:

    used-inventory/ford-focus-lx
    used-inventory/ford-focus
    aboutus/ourteam-andy-matthews

    The JS I'm using atm:

    var regex = /[^a-zA-Z0-9/-]/gi;
    var str = 'used inventory/ford/ding/dong';
    var final = str.replace(regex,'-');
    console.log(final);

    Can anyone help out?
    Adobe Certified Coldfusion MX 7 Developer
    Adobe Certified Advanced Coldfusion MX Developer
    My Blog (new) | My Family | My Freelance | My Recipes

  2. #2
    Function Curry'er JimmyP's Avatar
    Join Date
    Aug 2007
    Location
    Brighton, UK
    Posts
    2,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This should work:

    Code JavaScript:
    theString.replace(/[^\w]/g, function(m){
        if ( m === '/' && !arguments.callee.firstSlash ) {
            arguments.callee.firstSlash = true;
            return m;
        }
        return '-';
    });

    When the first slash is encountered the Boolean 'firstSlash' property of the function is changed to true, meaning that only the very first slash will be allowed, all others will be treated like regular non-alphanumeric characters (replaced with '-').
    Last edited by JimmyP; Mar 21, 2009 at 08:39.
    James Padolsey
    末末末末末末末末末末末末末末末末末末末
    Awesome JavaScript Zoomer (demo here)
    'Ajaxy' - Ajax integration solution (demo here)

  3. #3
    SitePoint Wizard creole's Avatar
    Join Date
    Oct 2000
    Location
    Nashvegas Baby!
    Posts
    7,845
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Whoa...had no idea the replace method took a callback function! I'll give this a shot JimmyP, thanks a lot!
    Adobe Certified Coldfusion MX 7 Developer
    Adobe Certified Advanced Coldfusion MX Developer
    My Blog (new) | My Family | My Freelance | My Recipes

  4. #4
    SitePoint Wizard creole's Avatar
    Join Date
    Oct 2000
    Location
    Nashvegas Baby!
    Posts
    7,845
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Err...not callback, but function as argument.
    Adobe Certified Coldfusion MX 7 Developer
    Adobe Certified Advanced Coldfusion MX Developer
    My Blog (new) | My Family | My Freelance | My Recipes

  5. #5
    SitePoint Wizard creole's Avatar
    Join Date
    Oct 2000
    Location
    Nashvegas Baby!
    Posts
    7,845
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Worked perfectly! This is what I've got:
    Code:
    var regex = /([^a-zA-Z0-9-])/gi;
    var str = 'used/inventory/ford/focus/lx';
    var final = str.replace(regex, function(m){
    	if ( m === '/' && !arguments.callee.firstSlash ) {
    		arguments.callee.firstSlash = true;
    		return m;
    	}
    	return '-';
    });
    console.log('src: '  + str);
    console.log('final '  + final);
    Adobe Certified Coldfusion MX 7 Developer
    Adobe Certified Advanced Coldfusion MX Developer
    My Blog (new) | My Family | My Freelance | My Recipes


Bookmarks

Posting Permissions

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