Monday, 8 August 2022

Regular Expressions In JS

 index.html file

<!DOCTYPE html>
<html lang="en">
<body>
<script>
let re;
re=/hello/i;      //case insensitive
re=/^hello/;      //must start with hello
re=/hello$/;      //must end with hello
re=/h.llo/;       //any char only once
re=/h*llo/;       //any char more than one time

re=/[ha]llo/;     //only h or a allowed(only 1)
re=/[^ha]llo/;    //any char except h or a allowed(only 1)
re=/[A-Za-z]llo/;   //any alphabet allowed(only 1)
re=/[0-9]llo/;      //any digit allowed(only 1)
re=/hel[a-z]{2}o/;  //any small letter, exactly 2 times
re=/hel[a-z]{1,3}o/;//any small letter,1-3 times.

const res=re.exec('allhello all');
console.log(res);
</script>
</body>
</html>





No comments:

Post a Comment

Regular Expressions In JS

 index.html file <! DOCTYPE html > < html lang = "en" > < body > < script > let re ; re = /hello/ i ;  ...