[일일코딩 #25] javascript String에서 String 찾기

Daily Codewars #25

Question

codewars link
Complete the solution so that it returns the number of times the search_text is found within the full_text.

searchSubstr( fullText, searchText, allowOverlap = true )

so that overlapping solutions are (not) counted. If the searchText is empty, it should return “0”. Usage examples:

searchSubstr('aa_bb_cc_dd_bb_e', 'bb') # should return 2 since bb shows up twice
searchSubstr('aaabbbcccc', 'bbb') # should return 1
searchSubstr( 'aaa', 'aa' ) # should return 2
searchSubstr( 'aaa', '' ) # should return 0
searchSubstr( 'aaa', 'aa', false ) # should return 1

My Solution

function searchSubstr(fullText, searchText, allowOverlap ){
  var count=0, sIdx=0;
  for(i in fullText){
    var cIdx = fullText.indexOf(searchText, sIdx);
    sIdx = allowOverlap==false? cIdx+searchText.length+1 : cIdx+1;
    if(cIdx<0) return count;
    count++;
  }
  return 0;
}

최대로 fullText.length만큼 for를 돈다.
거기서 allowOverlap에 따라 String.indexOf의 두번째 인자에 시작위치를 바꿔줘가며 돌린다.

@Azuaron’s Solution

function searchSubstr(fullText, searchText, allowOverlap) {
  if(searchText == '') return 0;
  var re = new RegExp(searchText, 'g');
  if(allowOverlap) {
    var count = 0;
    while(re.exec(fullText)) {count++; re.lastIndex -= searchText.length - 1;}
    return count;
  } else return (fullText.match(re) || []).length || 0;
}

false일땐 일단 정규표현식으로 간단히 리턴하고,
true면 re.lastIndex -= searchText.length - 1로 lastIndex를 바꿔준다.

@kumorig’s Solution

function searchSubstr( t, s, o ){
  return(t.length===0||s.length===0)?0:t.match(new RegExp((o||(o==undefined))?"(?=("+s+"))":t,"g")).length; 
}

헤헤…정규식…이전 회사에 다녔던 분 이름인 정규식…

Published by

Yurim Jin

아름다운 웹과 디자인, 장고와 리액트, 그리고 음악과 맥주를 사랑하는 망고장스터

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s