본문 바로가기
JavaScript/메소드

[javascript] 정규식 ^ , + ,[], match()

by Angry Stock 2023. 7. 11.
반응형
[abc]  : a 또는 b 또는 c
[^abc] : a 또는 b 또는 c 포함하지 않음
a+ : a가 1개 또는 그 이상
function solution1(string) {
  return string.match(/[^a-c]+/g) || ['EMPTY'];
}

function solution2(string) {
  var answer = string
    .replace(/[abc]+/g, ' ')
    .trim()
    .split(' ');
  return answer == '' ? ['EMPTY'] : answer;
}

const string = ['baconlettucetomato', 'abcd', 'cabab'];

for (let i = 0; i < string.length; i++) {
  console.log('solution1', solution1(string[i]));
  console.log('solution2', solution2(string[i]));
}

 

결과

반응형

댓글