判斷字符串是否包含指定字符是很經常使用的功能,好比說,註冊時用戶名限制不能輸入「管理員」,或者須要js判斷url跳轉連接是否包含某個關鍵詞等……javascript
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> </head> <body> <script type="text/javascript"> /*輸出的數字表明位置,若是值爲-1則表示沒有這個字符 *注意:字母的話區別大小寫 */ a ="大魚海棠的博客:http://www.cnblogs.com/leeLxj/" b = "魚" document.write(a.indexOf(b)+"<br/>"); b= "海棠" document.write(a.indexOf(b)+"<br/>"); b= "yu" document.write(a.indexOf(b)+"<br/>"); b= "tang" document.write(a.indexOf(b)+"<br/>"); </script> </body> </html>