문자열에서 문자 포함 여부 방법

minho·2022년 3월 3일
0

includes

string.includes(str)

ex

let a = "hi my name is...";
let b = "my";
console.log(a.includes(b)); //true

indexOf

string.indexOf(str)

ex

let a = "hi my name is...";
let b = "mh";
console.log(a.indexOf(b)); //-1

string이 해당 문자를 가지고 있지 않으면 -1을 반환한다.

search

string.search(str)

ex

let a = "hi my name is...";
let b = "mh";
console.log(a.search(b)); //-1

정규식

/str/.test(string)

ex

let a = "hi my name is...";
let b = "mh";
console.log(/b/.test(a));  //false

match

string.match(str)

ex

let a = "hi my name is...";
let b = "my";
console.log(a.match(b));  
//[ 'my', index: 3, input: 'hi my name is...', groups: undefined ]

match는 확인하려는 str의 index를 알려준다.

profile
Live the way you think

0개의 댓글