I used the following code for coderbyte:
function VowelCount(str) {
// code goes here
return str.match(/[aeiou]/gi).length;
}
// keep this function call here
// to see how to enter arguments in JavaScript scroll down
print(VowelCount(readline()));
I understand most of the code, except for the following parts:
- What do the forward slashes and the square brackets do?
- What does the
gi
do? - What is the difference between
search()
andmatch()
? What condition should I use what in?
I used the following code for coderbyte:
function VowelCount(str) {
// code goes here
return str.match(/[aeiou]/gi).length;
}
// keep this function call here
// to see how to enter arguments in JavaScript scroll down
print(VowelCount(readline()));
I understand most of the code, except for the following parts:
- What do the forward slashes and the square brackets do?
- What does the
gi
do? - What is the difference between
search()
andmatch()
? What condition should I use what in?
- developer.mozilla/en-US/docs/Web/JavaScript/Reference/… and developer.mozilla/en-US/docs/Web/JavaScript/Guide/… – Fabrício Matté Commented Sep 8, 2013 at 19:07
4 Answers
Reset to default 9Quoting the doc:
When you want to know whether a pattern is found in a string use
search
(similar to the regular expressiontest
method); for more information (but slower execution) usematch
(similar to the regular expressionexec
method).
In this case, it's not enough just to know that a vowel (either 'a', 'e', 'i', 'o', or 'u' - that's what's expressed with so-called character class expression, [aeiou]
) is in the string, as the purpose of the function is to count vowels.
So it scans the string, collecting all (that's what /g
modifier is for) the matches, regardless of character case (/i
), into an array, then returns the length of this array.
There's a bug in this function, however. As String.match returns null
if no matches were found, the function throws an error if param string has no vowels at all:
VowelCount('ddd'); // TypeError: Cannot read property 'length' of null
It has to be fixed with additional check for the match result.
function vowelCount(str) {
var vowels = str.match(/[aeiou]/gi);
return vowels ? vowels.length : 0;
}
The slashes delimit a regular expression (RegExp()
) literal (as opposed to using new RegExp()
notation), the square brackets are a series of characters for the regular expression to match.
References:
RegExp()
.
forward slashes wraps regular expression statement.
gi means search will be global (all occurances) and case insensitive.
There's several functions to search for a string in another string, the probably most famous ones being .indexOf()
, .search()
, .match()
.
str.indexOf(s)
Returns index (position) of first matching String s inside string str.
str.search(s)
Does what indexOf() does but allows s to be not just a string but a regular expression. (Actually, it even sees a 'normal' string as such).
str.match(s)
Is like search() but instead of returning the first occurence's index, it returns all occurence's as an array.
Now, regarding the
/[]/
and similar, this is (an example of) a before mentioned regular expression. More info on what does what can be found here. In short, it defines a string pattern to search for and how to search for it.