본문 바로가기
javaScript/JS Tutorial

[자바스크립트]endsWith() 끝나는 문자열 확인하기

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

endsWith()

endsWith()메서드는 문자열이 지정된 값으로 끝나면 true를 반환하고 그렇지 않으면 false를 반환

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

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

실행결과

true

text변수 안에 문장이 universe.로 끝나면 true 반환

 

 

검색 범위 지정하기

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

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

실행결과

true

문자열 처음부터 11번째까지가 "world"로 끝나면 true반환

 

 

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

728x90
반응형

댓글