這篇文章主要介紹了簡介JavaScript中search()方法的使用,是JS入門學習中的基礎知識,需要的朋友可以參考下
此方法執行搜索正則表達式,String對象之間的匹配。
語法
- string.search(regexp);
下面是參數的詳細信息:
regexp : 正則表達式對象。如果非RegExp對象obj傳遞,它是隱式通過使用new RegExp(obj) 轉換為RegExp
返回值:
如果成功的話,搜索將返回字符串內的正則表達式的索引。否則,它返回 -1.
例子:
- <html>
- <head>
- <title>JavaScript String search() Method</title>
- </head>
- <body>
- <script type="text/javascript">
- var re = /apples/gi;
- var str = "Apples are round, and apples are juicy.";
- if ( str.search(re) == -1 ){
- document.write("Does not contain Apples" );
- }else{
- document.write("Contains Apples" );
- }
- </script>
- </body>
- </html>
這將產生以下結果:
- Contains Apples
新聞熱點
疑難解答
圖片精選