37-10000 web入门 JavaScript 正则表达式

 

 

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
<!-- 
    search() 方法使用正则表达式 -->


<p>搜索字符串 runoob ,并显示匹配的起始位置:</p>
<!-- 加入点击事件的按钮  -->
<button onclick="myFunction()">点我 </button>
    <!--  给p段落标签加入属性  id属性  通过id属性找到HTML元素 p -->
<p id="demo"></p>


<script>
    function myFunction(){
        var str="Visit Runoob!";
        var n=str.search(/Runoob/i);
        document.getElementById("demo").innerHTML=n;
    }
</script>
</body>
</html>

效果;

   

 

posted @ 2021-11-20 11:49  优敏行  阅读(25)  评论(0)    收藏  举报