Skip to content

Instantly share code, notes, and snippets.

@QuadFlask
Created April 26, 2016 14:00
Show Gist options
  • Save QuadFlask/fd8912477f9a86a180b8986dda99e70d to your computer and use it in GitHub Desktop.
Save QuadFlask/fd8912477f9a86a180b8986dda99e70d to your computer and use it in GitHub Desktop.
[CodeWars] strp comments

코맨트를 지우는 기능

그냥 정규식으로 하는게 더 쉬워 보이긴 함

MySolution

var solution = (input, markers)=> input.split('\n')
  .map(l=> 
    l.substr(0, markers
      .map(l.indexOf)
      .filter(i=> i>0)
      .reduce((a,b)=> Math.min(a,b), Number.MAX_VALUE)).trim())
  .join('\n');

Best practice

function solution(input, markers){
  return input.replace(new RegExp("\\s?[" + markers.join("") + "].*(\\n)?", "gi"), "$1");
}

$1 은 정규식 매칭 후, 그룹된 녀석들을 지칭하는 키워드($1, $2 이런식으로 1부터 시작)로, replace 할때 써먹으면 편리한듯??

\\s?    공백 문자
[" + markers.join("") + "]    마커용 문자들
.*      모든 문자
(\\n)?   그룹으로 이녀석이 선택($1)

공백문자 + 마커문자중 하나 를 기준으로 보면 될듯

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment