일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
- Event
- HTML
- modal
- Empty
- Review
- javascript
- for loop
- yet
- CSS
- Dom
- innerText
- react
- innerHTML
- node.js
- addEventListener
- ES5
- TypingEffect
- nodeValue
- beforeinput
- dotenv
- keyboardEvent
- Temporal dead zone
- css:position
- a11y
- keyup
- js
- es6
- UI
- textContent
- node
- Today
- Total
the murmurous sea
JS: match() vs includes() 본문
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 |
: 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 |