/정규식/.test(문자열) 이용해서 해당 문자열이 특정 패턴을 만족하는지 확인할 수 있다.
참고로 문자열.match(/정규식/) 을 이용한 방법도 있다.
https://myeonguni.tistory.com/1555
글 좋은 것 같다..
다음 모든 코드의 결과 true로 나온다.
<script>
const regex = /^abc$/;
const str = "abc";
console.log(regex.test(str));
</script>
<script>
const regex = /^[0-9]+$/;
const str = "123";
console.log(regex.test(str));
</script>
<script>
const regex = /^[0-9]+[0-9a-zA-Z]*[a-zA-Z]+$/;
const str = "12a33a";
console.log(regex.test(str));
</script>
<script>
const regex = /^[ㄱ-ㅎㅏ-ㅣ가-힣]+$/;
const str = "가ㄱㅁㅋㅏㅜ";
console.log(regex.test(str));
</script>
<script>
const regex = /^1[0-9]{2}$/;
const str = "123";
console.log(regex.test(str));
</script>