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
  1. #1
    New to the CF scene
    Join Date
    Oct 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Smile Regular expression and user input string

    Hello,

    What i intend on achieving should be simple if i could just understand regular expressions.

    var names=new Array('ben','chris','mary','john');

    If a user input string is

    var str= "1: said that there are 2 kings in 3:'s house. Tell 4: to come quickly and bring gifts as well."

    Using regular expressions, I want to replace the numbers and column combinations with the corresponding name.

    so 1: show be replaced with ben, 3: should be replaced with mary.

    So new returned string should be

    var newStr="ben said there are 2 kings in mary's house. Tell john to come quickly and bring gifts as well."

    How do I get to match the number column pattern and get the matched string replaced with names[number].

    function (names, str){//function accepts names array and str to replace

    //regex matching and replacing

    //returning new str

    return newStr;
    }

    any help will be much appreciated.

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,133
    Thanks
    12
    Thanked 332 Times in 328 Posts
    pretty much taken from the MDC page
    Code:
    function vsprintf(str, arr)
    {
        function vprint(match, part)
        {
            // though without type checking ...
            return arr[part - 1];
        }
        // though I'd rather go for %1 than 1:
        return str.replace(/(\d):/g, vprint);
    }
    Last edited by Dormilich; 10-19-2010 at 02:02 PM. Reason: some improvements
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • Users who have thanked Dormilich for this post:

    barnabas1 (10-19-2010)

  • #3
    New to the CF scene
    Join Date
    Oct 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Smile

    If the string had a double digit before column eg 11: . The function returned

    names[1-1] ; instead of names[11-1]; So code limited to between 1 to 9;

    Also if it was a stringwith 10: it would cos an error cos it would try to return names[0-1];

    so made some modifications to cater for this situations. also if the array position is undefined, it should return the matched pattern back.

    function vsprintf(str, arr)
    {
    function vprint(match, part)
    {
    // though without type checking ...
    if(part>=1 && part<=arr.length){
    //IF part is 0 and not greater than array length,

    var h=arr[part - 1];
    }

    if(!h){//IF h is undefined return the match pattern back
    h=part+':'; //just precaution. shouldnt be necessary
    }

    return h;
    }
    // though I'd rather go for %1 than 1: (WHAT IS THE REGEX FOR THIS)

    return str.replace(/(\d):/g, vprint);
    }

    var str= "1: said that there are 10 kings in 13:'s house. Tell 4: to come 10: quickly and bring gifts as well."

    var names=new Array('ben','chris','mary','john','jane','keisha','ellan','joise','bukky','janet','christopher');

    var str2=vsprintf(str, names);

    document.write(str2);

    printed to screen

    "ben said that there are 10 kings in 1mary's house. Tell john to come 10: quickly and bring gifts as well."

    Would love it if it could match double digits as well. but i guess this is sufficient thanks.
    Actually %1 maybe better. but it should also match %12 (the double digit).

    So what is the code for matching %1 and %12 instead.
    So matching all digits attached to special character

  • #4
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,133
    Thanks
    12
    Thanked 332 Times in 328 Posts
    just a matter of adjusting the RegExp: /(\d+):/g

    Edit:
    // though I'd rather go for %1 than 1: (WHAT IS THE REGEX FOR THIS)
    /%(\d+)/g


    Quote Originally Posted by barnabas1 View Post
    so made some modifications to cater for this situations. also if the array position is undefined, it should return the matched pattern back.

    Code:
                   function vprint(match, part)
                    {
                        // though without type checking ...
                        if(part>=1 && part<=arr.length){
         //IF part is 0 and not greater than array length, 
                       
                        var h=arr[part - 1];
                        }
    
                        if(!h){//IF h is undefined return the match pattern back
                            h=part+':'; //just precaution. shouldnt be necessary
                        }
    
                        return h;
                    }
    if(!h) will also fire if h is null, 0 or "". better use
    Code:
    function vprint(match, part)
    {
        if(part > 0 && part <= arr.length) {
         //IF part is 0 and not greater than array length, 
            return arr[part - 1];
        }
        return match; // preferably return ""; (more consistent with the expectations)
    }
    Last edited by Dormilich; 10-20-2010 at 07:21 AM.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #5
    New to the CF scene
    Join Date
    Oct 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    thanks. I changed my choice to matching %1 instead.
    Thanks .
    I started a new thread titled url pattern and regex. pls do check it out.


  •  

    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
    •