如何在多个正则表达式中重用通用模式?

时间:2019-12-11 17:03:43

标签: javascript regex dry

是否可以只写一次BCC Fans community来写这样的指令?

if (document.title.match(/(.+?) - BCC Fans community$/)) {
    document.title = document.title.match(/(.+?) - BCC Fans community$/)[1]
} else if (document.title.match(/^BCC Fans community - (.+)/)) {
    document.title = document.title.match(^/BCC Fans community - (.+)/)[1]
}

1 个答案:

答案 0 :(得分:1)

将它们写为字符串,然后使用新的RegExp(_STRING_);参见this