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 6 of 6
  1. #1
    New Coder
    Join Date
    Oct 2012
    Posts
    28
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Creating a function to show a substring is present in the string

    hi i have called my function
    [icode]match1(string,pattern)[icode]

    i want to create a function where if the pattern is present in the string it will return true

    Any tips on how to tackle this problem?

    here is my code so far
    [code]function match1(string,pattern)
    {if(string==pattern)
    return true;
    else
    return false;
    }[code]

    i need to change the if statement but im having trouble also im trying to make it case insensitive
    Last edited by nervousprogramm; 11-04-2012 at 12:38 PM.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,992
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    Quote Originally Posted by nervousprogramm View Post
    hi i have called my function
    [icode]match1(string,pattern)[icode]

    i want to create a function where if the pattern is present in the string it will return true

    Any tips on how to tackle this problem?

    here is my code so far
    [code]function match1(string,pattern)
    {if(string==pattern);
    return true;
    else
    return false;
    }[code]

    i need to change the if statement but im having trouble also im trying to make it case insensitive
    Code:
    <script type = "text/javascript">
    
    var string = "the quick brown fox";
    
    if (/\bbrown\b/gi.test(string)) {   // \b switch makes the search for a whole word only.  g switch means global search.  i switch means case insensitive.
    alert ("The pattern was found");
    }
    else {
    alert ("The pattern was not found")
    }
    
    </script>
    Another example using match

    Code:
    <script type = "text/javascript">
    
    var str="The man is at a dance, dancing, but he will not marry or tarry with Harry, nor has he ever tarried with Harry, nor will he be found to be tarrying"; 
    var pattern=/dance|dancing|\btarry\b|tarried|tarrying/gi;
    alert(str.match(pattern));
    var pattern = /\bdanc\w+|\b[a-z]+arr\w+/gi;
    alert(str.match(pattern));
    var pattern = /\bdanc\w+|\b[a-gj-z]+arr\w+/gi;
    alert(str.match(pattern));
    var pattern = /\b(tarr[iy](\w+))/gi;
    alert(str.match(pattern));
    var pattern = /\b(tarr[iy](\w+)?)/gi;
    alert(str.match(pattern));
    
    </script>


    Quizmaster: What character in A.A.Milne's "Winnie The Pooh" is named after his son?
    Contestant: Pooh.
    Last edited by Philip M; 11-04-2012 at 12:59 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    nervousprogramm (11-04-2012)

  • #3
    New Coder
    Join Date
    Oct 2012
    Posts
    28
    Thanks
    17
    Thanked 0 Times in 0 Posts
    That has confused me even more =(

    I need to create a functoin so when i type alert( match1("HELLO","hell")
    would return true

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,992
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    Quote Originally Posted by nervousprogramm View Post
    That has confused me even more =(

    I need to create a functoin so when i type alert( match1("HELLO","hell")
    would return true

    Code:
    var str= "HELLO";
    var pattern = /hell/gi;  // a regular expression
    if (str.match(pattern)) {  // if the pattern is found (case insensitive)
    alert ("true");
    }
    else {
    alert ("false");
    }
    Last edited by Philip M; 11-04-2012 at 01:10 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    nervousprogramm (11-04-2012)

  • #5
    New Coder
    Join Date
    Oct 2012
    Posts
    28
    Thanks
    17
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    Code:
    var str= "HELLO";
    var pattern = /hell/gi;  // a regular expression
    if (str.match(pattern)) {  // if the pattern is found (case insensitive)
    alert ("true");
    }
    else {
    alert ("false");
    }
    Ok so heres my code
    Code:
    function match1(string,pattern)
    {if (string.match(pattern))
    return true;
    else
    return false;
    }
    
    alert(match1("HELLO","hell"))
    that should return true but its returning false =S

  • #6
    New Coder
    Join Date
    Oct 2012
    Posts
    28
    Thanks
    17
    Thanked 0 Times in 0 Posts
    i have solved this problem thank you all for you input


  •  

    Posting Permissions

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