JS正则表达式、while循环

<!--
while循环
while 循环会在指定条件为真时循环执行代码块。
语法:
while (条件)
{
需要执行的代码
}
do/while 循环是 while 循环的变体。该循环会在检查条件是否为真之前执行一次代码块,
然后如果条件为真的话,就会重复这个循环。
语法:
do
{
需要执行的代码
}
while (条件);
while 和 do/while 的区别 : do/while至少会执行一遍

4钟循环的使用场景:
for:遍历数组、字符串,for ··in:遍历对象,while:与for差不多遍历条件要满足,
do while 至少执行一遍循环,遍历数组与字符串。
------------------
break:打破循环
continue:中断此次循环继续下次循环
------------------
typeof 操作符:用来检测变量的数据类型
typeof "John" // 返回 string
typeof 3.14 // 返回 number
typeof false // 返回 boolean
typeof [1,2,3,4] // 返回 object
typeof {name:'John', age:34} // 返回 object
null:是一个只有一个值的特殊类型。表示一个空对象引用。
undefined :是一个没有设置值的变量。
undefined与null区别:类别不同,值相同
typeof undefined // undefined
typeof null // object
null === undefined // false
null == undefined // true
正则表达式
语法:
/正则表达式主题/修饰符
search()检索字符串
var str="end the";
var n=str.search(/end/i);
-->

<script type="text/javascript">
            var str="end the";
            var n=str.search(/end/i);
            document.write(n+"<br/>");
            //replace()替换
            var t=str.replace(/end/i,"tnt");
            document.write(t+"<br/>");
            //test()是否存在,返回true/false
            //exec()返回查询字符,未查询到返回null
            //哦安短是否为数字、字母、下划线
            function isValid(str) { return /^\w+$/.test(str); }
            str = "1234abd__"
            document.write(isValid(str));
            document.write("<br>");
            
            str2 = "$32343#"
            document.write(isValid(str2));
            document.write("<br>");
            //判断是否全部为字母
            val = "123456"
            var isletter = /^[a-zA-Z]+$/.test(val);
            document.write(isletter);
            document.write("<br>");
            
            val2 = "asaaa"
            var isletter2 = /^[a-zA-Z]+$/.test(val2);
            document.write(isletter2+"<br/>");
            //判断是否全是数字
            val = "123456"
            var isnum = /^\d+$/.test(val);
            document.write(isnum);
            document.write("<br>");
            
            val2 = "as123"
            var isnum2 = /^\d+$/.test(val2);
            document.write(isnum2);
        </script>

 

 

posted @ 2022-11-22 17:02  山君与见山  阅读(157)  评论(0编辑  收藏  举报