JavaScript搜索字符串并將搜索結(jié)果返回到字符串的方法

字號(hào):


    本文實(shí)例講述了JavaScript搜索字符串并將搜索結(jié)果返回到字符串的方法。分享給大家供大家參考。具體如下:
    javascript操作字符串帶有一個(gè)match方法用于搜索字符串,如果找到指定的字符串則返回搜索字符串,如果未找到則返回null,match方法區(qū)分大小寫
    <!DOCTYPE html>
    <html>
    <body>
    <script>
    var str="Hello world!";
    document.write(str.match("world") + "<br>");
    document.write(str.match("World") + "<br>");
    document.write(str.match("worlld") + "<br>");
    document.write(str.match("world!"));
    </script>
    </body>
    </html>
    返回結(jié)果:
    world
    null
    null
    world!