JavaScript檢查子字符串是否在字符串中的方法
更新時間:2016年02月03日 09:26:58 作者:皮蛋
這篇文章主要介紹了JavaScript檢查子字符串是否在字符串中的方法,涉及JavaScript查詢、正則匹配等操作技巧,需要的朋友可以參考下
本文實例講述了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相關內(nèi)容感興趣的讀者可查看本站專題:《JavaScript查找算法技巧總結》及《JavaScript數(shù)據(jù)結構與算法技巧總結》
希望本文所述對大家JavaScript程序設計有所幫助。
相關文章
使用JavaScript和CSS實現(xiàn)簡單的字符計數(shù)器
在本文中,你將學習如何使用?JavaScript?創(chuàng)建字符計數(shù)器。計數(shù)的數(shù)字可以在小顯示屏中看到。文中示例代碼講解詳細,感興趣的小伙伴可以了解一下2022-08-08
微信小程序van-field中的left-icon屬性自定義實現(xiàn)過程
在小程序中,我們是用 Vant 組件庫時,常常會用到 van-field 輸入框控件,今天我將跟大家分享的是 van-field 輸入框控件中的 left-icon 屬性的自定義怎么實現(xiàn),感興趣的朋友一起看看吧2023-08-08
深入淺析JavaScript系列(13):This? Yes,this!
在這篇文章里,我們將討論跟執(zhí)行上下文直接相關的更多細節(jié)。討論的主題就是this關鍵字。實踐證明,這個主題很難,在不同執(zhí)行上下文中this的確定經(jīng)常會發(fā)生問題2016-01-01
javascript setAttribute, getAttribute 在不同瀏覽器上的不同表現(xiàn)
該方法把指定的屬性設置為指定的值。如果不存在具有指定名稱的屬性,該方法將創(chuàng)建一個新屬性。2010-08-08

