首頁 > web前端 > js教程 > JavaScript檢查子字串是否在字串中的方法_javascript技巧

JavaScript檢查子字串是否在字串中的方法_javascript技巧

WBOY
發布: 2016-05-16 15:16:19
原創
1263 人瀏覽過

本文實例講述了JavaScript檢查子字串是否在字串中的方法。分享給大家參考,具體如下:

var strnew="Hello Raghu How are u"
//Checking existence in entire string
if(strnew.indexOf("Raghu") != -1 )
{
  alert("Exists");
}
/*
Checking existence at the end of string
For example, variable result1 in the following example is assigned 
the value of true because the word "Ishmael" appears at the end of 
the string. Variable result2 is assigned a value of false because 
"Raghu" doesn't appear at the end of the string.
*/
result1 = /Raghu$/.test("Call me Raghu");
result2 = /Raghu$/.test("Call me Raghu. Some years ago");
/*
Checking existence in the begining of string
In the following example, variable result1 is assigned the value 
of true because the word "Birds" appears at the beginning of the 
string. Variable result2 is assigned the value of false because 
"Birds" doesn't appear at the beginning of the string.
*/
result1 = /^Birds/.test("Birds fly");
result2 = /^Birds/.test("Big Birds fly");

登入後複製

更多關於JavaScript相關內容有興趣的讀者可查看本站專題:《JavaScript查找演算法技巧總結》及《JavaScript資料結構與演算法技巧總結

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

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板