test() เป็น regex method ค้นหารูปแบบในสตริง คืน true ถ้าเจอ false ถ้าไม่เจอ เป็น case-sensitive
ตัวอย่างที่ 1: พบรูปแบบ "Tu"
<html> <body>
<p id="text">Tutorix is the best e-learning platform</p>
<p id="test"></p>
<script>
var text = document.getElementById("text").innerHTML;
document.getElementById("test").innerHTML = /Tu/.test(text);
</script> </body> </html>
ผลลัพธ์
Tutorix is the best e-learning platform true
ตัวอย่างที่ 2: ไม่พบ "tu" (case-sensitive)
<html> <body>
<p id="text">Tutorix is the best e-learning platform</p>
<p id="test"></p>
<script>
var text = document.getElementById("text").innerHTML;
document.getElementById("test").innerHTML = /tu/.test(text);
</script> </body> </html>
ผลลัพธ์
Tutorix is the best e-learning platform false