String相关操作

xiaoxiao2022-06-11  24

​ <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> <p id="p0">这里是段落0</p> <p id="p1">这里是段落1</p> <p id="p2">这里是段落2</p> <p id="p3">这里是段落3</p> <p id="p4">这里是段落4</p> <p id="p5">这里是段落5</p> <p id="p6">这里是段落6</p> <script> //在字符串中查找字符串 var message="hello world! hello"; var str=message.indexOf("hello"); document.getElementById("p0").innerHTML=str; //lastindexOf() document.getElementById("p1").innerHTML=message.lastIndexOf("hello"); //替换字符串 var str2=message.replace("world","hello1"); document.getElementById("p2").innerHTML=str2; //match匹配字符串,如果匹配到则返回该字符串 var x=message.match("world"); document.getElementById("p3").innerHTML=x; </script> </body> </html> ​

 

转载请注明原文地址: https://www.6miu.com/read-4930344.html

最新回复(0)