|
-
Aug 5th, 2002, 03:06 PM
#1
Thread Starter
Hyperactive Member
JavaScript No of String
Using JavaScript:
What is the function that I can use to calculate how many string found in the string..
if Text="how are you . I hope you are fine"
I want to know how many (are) string in this string
output Here:
2
-
Aug 6th, 2002, 04:07 AM
#2
Code:
function GetOccurances(str){
var m = /are/g; //word to find RegExp
var oStr = new String(str);
if( oStr.search(m) > -1){ //at least on occurance
var array = oStr.match(m);
return array.length
}
return 0;
}
-
Aug 6th, 2002, 04:37 AM
#3
Thread Starter
Hyperactive Member
Thanks
But try this code:
[CODE]
<Script language=JavaScript>
function GetOccurances(str,Find)
{
var m =RegExp (Find,"gi");
var oStr = new String(str);
var No = oStr.match(m);
return No.length
return Find
}
X=GetOccurances("hor [CODE] [CODE] ARE [CODE] you are hi are","
Code:
");
document.write (X);
</Script>
When I calculate (Code) word it give me right output but when I calculate [CODE] or any word contain [ and ] it give me error output .... Why ? How can I solve this problem
Last edited by prokhaled; Aug 6th, 2002 at 05:42 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|