此方法執行搜索正則表達式,String對象之間的匹配。
語法
string.search(regexp);
下面是參數的詳細信息:
返回值:
例子:
<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