Results 1 to 3 of 3

Thread: JavaScript No of String

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    259

    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

  2. #2
    Frenzied Member DeadEyes's Avatar
    Join Date
    Jul 2002
    Posts
    1,196
    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;
    }

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    259

    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
  •  



Click Here to Expand Forum to Full Width