본문 바로가기
javaScript/JS Tutorial

[javaScript]includes() 문자열에 지정된 값 포함 되어있는지 확인

by mooyou 2022. 9. 25.
728x90
300x250

includes()

문자에 지정된 값이 포함되어 있으면 true를 반환하고 없으면 false를 반환한다.

 

<p id="demo"></p>

<p>The includes() method is not supported in Internet Explorer.</p>

<script>
    let text = "Hello world, welcome to the universe.";
    document.getElementById("demo").innerHTML = text.includes("world");
</script>

실행결과

true

 

 

 

검색 시작위치 지정하기

<p id="demo"></p>

<p>The includes() method is not supported in Internet Explorer.</p>

<script>
    let text = "Hello world, welcome to the universe.";
    document.getElementById("demo").innerHTML = text.includes("world", 12);
</script>

실행결과

false

12위치에서 검색 시작 world가 있으면 true 없으면 false

 

 

참고 : https://www.w3schools.com/js/js_string_search.asp

728x90
반응형

댓글