...

View Full Version : Creating a function to show a substring is present in the string



nervousprogramm
11-04-2012, 12:26 PM
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

Philip M
11-04-2012, 12:52 PM
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


<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


<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.

nervousprogramm
11-04-2012, 12:58 PM
That has confused me even more =(

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

Philip M
11-04-2012, 01:07 PM
That has confused me even more =(

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



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");
}

nervousprogramm
11-04-2012, 01:30 PM
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

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

nervousprogramm
11-04-2012, 02:10 PM
i have solved this problem thank you all for you input



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum