Q220634

by jun68ykt

HTML

<script src="https://rawcdn.githack.com/firebug/firebug-lite/firebug1.5/build/firebug-lite-debug.js"></script>

JavaScript

const FIXEDSTR = 'hoge';

function regexTest(str){

  let ary = str.split(/\s+/);

  if (ary.length > 1) {
    if (ary[0] === FIXEDSTR) {
      ary.shift();
      ary[0] = `${FIXEDSTR} ${ary[0]}`;
    }
    ary = ary.map((s, i) => i%2 ? null : `${s} ${FIXEDSTR}`).filter(s => s);
  }

  return ary.length === 1 ? ary[0] : ary;
}

// pattern1
console.log(regexTest('0:18 hoge')); // => "0:18 hoge"

// pattern2
console.log(regexTest('hoge 0:34 hoge')); // => "hoge 0:34 hoge"

// pattern3
console.log(regexTest('0:18 hoge 0:34 hoge')); // => "[ '0:18 hoge', '0:34 hoge' ]"