Ask Ben: Javascript String Replace Method

// This counts the number of times a string matching the
// given regular expression can be found in the given text.
function CountValue( strText, reTargetString ){
	var intCount = 0;
	 
	// Use replace to globally iterate over the matching
	// strings.
	strText.replace(
		reTargetString,
		 
		// This function will get called for each match
		// of the regular expression.
		function(){
			intCount++; 
		}
	);
	 
	// Return the updated count variable.
	return( intCount );
}

For Cut-and-Paste