最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Using .match and the difference between .search - Stack Overflow

programmeradmin5浏览0评论

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:

  1. What do the forward slashes and the square brackets do?
  2. What does the gi do?
  3. What is the difference between search() and match()? 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:

  1. What do the forward slashes and the square brackets do?
  2. What does the gi do?
  3. What is the difference between search() and match()? What condition should I use what in?
Share Improve this question edited Aug 27, 2021 at 18:26 Tot Zam 8,74611 gold badges54 silver badges79 bronze badges asked Sep 8, 2013 at 19:06 user2755667user2755667 1
  • 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
Add a ment  | 

4 Answers 4

Reset to default 9

Quoting the doc:

When you want to know whether a pattern is found in a string use search (similar to the regular expression test method); for more information (but slower execution) use match (similar to the regular expression exec 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.

发布评论

评论列表(0)

  1. 暂无评论