2013-04-21 2 views
0

문자열이 'a'이고 'a' 문자열 배열을 가진 모든 결과를 원합니다.자바 스크립트에서 문자열 배열의 문자열을 검색하려면 어떻게해야합니까?

var searchquery = 'a'; 
var list = [temple,animal,game,match, add]; 

나는 그것을 달성 할 수있는 자신의 name.how의 일부로 'a'result = [animal,game,match,add]; 모든 요소를 ​​원하는?

당신은 목록 filter
+0

에 대한 것'regex'를 사용하여 이 간단한 문제에 대한 잔인 함. – Anirudha

+0

감사합니다, anirudh .. – Anshul

답변

2

:

var searchquery = 'a'; 
var list = ['temple', 'animal', 'game', 'match', 'add']; 
var results = list.filter(function(item) { 
    return item.indexOf(searchquery) >= 0; 
}); 
// results will be ['animal', 'game', 'match', 'add'] 

(당신이 list 배열의 문자열을 인용 할 필요가 있습니다.)

+0

그 어떤 정규식? – Anshul

+1

스냅! :) 부도덕 한 작업에 추가 시간이 필요합니다 : P – Xotic750

+2

정규식으로 할 수있는 이유는 무엇입니까? – Xotic750

4
<div id="display"></div> 

var searchquery = 'a'; 
var list = ["temple", "animal", "game", "match", "add"]; 
var results = list.filter(function(item) { 
    return item.indexOf(searchquery) >= 0; 
}); 

document.getElementById("display").textContent = results.toString(); 

jsfiddle

관련 문제