JavaScript / Object / String.endsWith() / 특정 문자열로 끝나는지 확인하는 메서드
목차
.endsWith()
.endsWith()는 문자열이 특정 문자열로 끝나는지 확인한다. IE는 Edge부터 지원한다.
문법
string.endsWith( searchString, length )
- searchString : 검색할 문자열로 필수 요소이다. 대소문자를 구분한다.
- length : 문자열 중 어디까지 검색할지 정한다. 선택 요소로, 값이 없으면 전체 문자열을 대상으로 한다.
예제
- abcde가 e로 끝나는지 검사한다. e로 끝나므로 true를 반환한다.
'abcde'.endsWith( 'e' )
- abc가 e로 끝나는지 검사한다. e로 끝나지 않으므로 false를 반환한다.
'abcde'.endsWith( 'e', 3 )
- 는 abcdE가 de로 끝나는지 검사한다. 대소문자를 구분하므로 false를 반환한다.
'abcdE'.endsWith( 'de' )
<!doctype html> <html lang="ko"> <head> <meta charset="utf-8"> <title>JavaScript</title> <style> body { font-family: Consolas, monospace; } </style> </head> <body> <script> document.write( "<p>'abzcdz'.endsWith( 'z' ) : " + 'abzcdz'.endsWith( 'z' ) + "</p>" ); document.write( "<p>'abzcdz'.endsWith( 'z', 2 ) : " + 'abzcdz'.endsWith( 'z', 2 ) + "</p>" ); document.write( "<p>'abzcdz'.endsWith( 'z', 3 ) : " + 'abzcdz'.endsWith( 'z', 3 ) + "</p>" ); </script> </body> </html>