Go Back   CodingForums.com > :: Client side development > JavaScript programming > DOM and JSON scripting

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-19-2010, 02:23 AM   PM User | #1
barnabas1
New to the CF scene

 
Join Date: Oct 2010
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
barnabas1 is an unknown quantity at this point
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.
barnabas1 is offline   Reply With Quote
Old 10-19-2010, 01:54 PM   PM User | #2
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,880
Thanks: 9
Thanked 291 Times in 287 Posts
Dormilich is on a distinguished road
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);
}
__________________
please post your code wrapped in [CODE] [/CODE] tags

Last edited by Dormilich; 10-19-2010 at 02:02 PM.. Reason: some improvements
Dormilich is offline   Reply With Quote
Users who have thanked Dormilich for this post:
barnabas1 (10-19-2010)
Old 10-19-2010, 11:20 PM   PM User | #3
barnabas1
New to the CF scene

 
Join Date: Oct 2010
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
barnabas1 is an unknown quantity at this point
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
barnabas1 is offline   Reply With Quote
Old 10-20-2010, 06:56 AM   PM User | #4
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,880
Thanks: 9
Thanked 291 Times in 287 Posts
Dormilich is on a distinguished road
just a matter of adjusting the RegExp: /(\d+):/g

Edit:
Quote:
// 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)
}
__________________
please post your code wrapped in [CODE] [/CODE] tags

Last edited by Dormilich; 10-20-2010 at 07:21 AM..
Dormilich is offline   Reply With Quote
Old 10-20-2010, 03:57 PM   PM User | #5
barnabas1
New to the CF scene

 
Join Date: Oct 2010
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
barnabas1 is an unknown quantity at this point
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.
barnabas1 is offline   Reply With Quote
Reply

Bookmarks

Tags
regular expressions, user input

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:02 AM.


Advertisement
Log in to turn off these ads.