>  기사  >  웹 프론트엔드  >  string_javascript 팁에 하위 문자열이 있는지 확인하는 JavaScript 방법

string_javascript 팁에 하위 문자열이 있는지 확인하는 JavaScript 방법

WBOY
WBOY원래의
2016-05-16 15:16:191198검색

이 기사의 예에서는 문자열에 하위 문자열이 있는지 확인하는 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 프로그래밍에 종사하는 모든 사람에게 도움이 되기를 바랍니다.

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.