JS note 使用 regex 改變單雙引號
eloquent javascript 的練習題 :
將字串對話 'I'm the cook,' he said, 'it's my job.'
其中的單引號改成雙引號,但略過縮讀字 contraction 的單引號,如: I'm
或者 It's
修改來幾次還是不得其解,偷看了這篇 gist ,修改出答案:
let text = "'I'm the cook,' he said, 'it's my job.'";
console.log(`${text} →`);
// → "I'm the cook," he said, "it's my job."
console.log(text.replace(/(^)'|(\W)'|'(\W)|'($)/g, '$2"$3'));
submatch $1
, $2
, $3
是根據 regex 字串的左小括弧出現的順序決定,其中是否出現符號 |
無關,這是我做了習題才弄清楚的概念
.
reply_count: 0
get_replies : 0
.