the murmurous sea

JS: match() vs includes() 본문

#dev/개념정리

JS: match() vs includes()

lunacer 2020. 6. 24. 13:38

includes()

String.prototype.includes()

: method determines whether one string may be found within another string, returning true or false as appropriate.
: Polyfill(method) has been added to the ECMAScript 2015 specification and may not be available in all JavaScript implementations yet. However, you can easily polyfill this method.

 

Syntax

str.includes(searchString[, position])

- searchString : A string to be searched for within str.

- position [optional]

  : The position within the string at which to begin searching for searchString.

  : Defaults to 0.

 

Return
  : true if the search string is found anywhere within the given string; otherwise, false if not.

 

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes

1. method

2. The includes() method is case sensitive.

 

Array.prototype.includes()

: method determines whether an array includes a certain value among its entries, returning true or false as appropriate.

 

Syntax

arr.includes(valueToFind[, fromIndex])

- valueToFind : The value to search for.

- fromIndex [optional]

   : The position in this array at which to begin searching for valueToFind.

   : Defaults to 0
   : Negative values: using the absolute value of fromIndex as the number of characters from the end of the array at.

 

Return

: A Boolean which is true if the value valueToFind is found.
: Values of zero are all considered to be equal, regardless of sign.
  - That is, -0 is considered to be equal to both 0 and +0), but false is not considered to be the same as 0.
  - Note: Technically speaking, includes() uses the sameValueZero algorithm to determine whether the given element is found.

 

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes

match()

String.prototype.match()

1. method 
2. Retrieves the result of matching a string against a regular expression.

 

Syntax

str.match(regexp)

: If regexp is a non-RegExp object, it is implicitly converted to a RegExp by using new RegExp(regexp).
: If you don't give any parameter and use the match() method directly, you will get an Array with an empty string: [""]

 

Return

: An Array 

: whose contents depend on the presence or absence of the global (g) flag, or null if no matches are found.

  - If the g flag is used, all results matching the complete regular expression will be returned, but capturing groups will not.
  - If the g flag is not used, only the first complete match and its related capturing groups are returned. In this case, the returned item will have additional properties as described below.

groups
: An object of named capturing groups whose keys are the names and values are the capturing groups or
 undefined if no named capturing groups were defined. See Groups and Ranges for more information.

index
: The index of the search at which the result was found.

input
: A copy of the search string.

: If the regular expression does not include the g flag, str.match() will return the same result as RegExp.exec().

 

methods

RegExp.test() If you need to know if a string matches a regular expression RegExp
RegExp.exec() If you only want the first match found
RegExp.exec() or String.prototype.matchAll() If you want to obtain capture groups and the global flag is set

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match

'#dev > 개념정리' 카테고리의 다른 글

setTimeout vs. setInterval  (0) 2020.06.30
json.parse() vs. response.json  (0) 2020.06.25
JS: instance?  (0) 2020.06.10
JS ES6: Array.from()  (0) 2020.06.05
Event: keydown, keypress, keyup  (0) 2020.06.05
Comments