Javascript 如何在多个正则表达式中重用公共模式?
是否可以只编写一次BCC Fans community来编写此类指令Javascript 如何在多个正则表达式中重用公共模式?,javascript,regex,dry,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.ti
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]
}
将它们写为字符串,然后使用new
RegExp(\u string\u)代码>请参见只要将正则表达式组合到^BCC Fans community-(.+)|(.+?)-BCC Fans community$
中,然后您的结果就是m[1]| m[2]
如果m
@ctwheels,除了Eric的答案之外,这很有用。非常感谢。