Q168504

by jun68ykt

HTML

<script src="https://getfirebug.com/firebug-lite-debug.js"></script>

JavaScript

const maskingWordsInSpeech = (text) => {
  let x = text;
  while(true) {
    const y = x.replace(/[^a-z\(\[][a-z]+[^a-z\)\]]/i, 
      str => str.replace(/[a-z]/ig, '_')
    );
    if (x == y) break;
    x = y;
  }
  return x;
};

// 以下、テスト
[
  '-[TOM] ♪ Taking care to keep my baggage with me ♪',
  '-(banging) Huh?',
  '- Are you hungry?',
  '-[SNOOPY] I AM HunGry. (Sigh)'
].forEach(
  text => console.log(maskingWordsInSpeech(text))
);