首頁  >  文章  >  web前端  >  JavaScript搜尋字串並將搜尋結果傳回字串的方法_javascript技巧

JavaScript搜尋字串並將搜尋結果傳回字串的方法_javascript技巧

WBOY
WBOY原創
2016-05-16 16:05:271503瀏覽

本文實例講述了JavaScript搜尋字串並將搜尋結果傳回字串的方法。分享給大家供大家參考。具體如下:

javascript操作字串帶有一個match方法用於搜尋字串,如果找到指定的字串則傳回搜尋字串,如果找不到則傳回null,match方法區分大小寫

<!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>

回傳結果:

world
null
null
world!

希望本文所述對大家的javascript程式設計有所幫助。

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn