Search code examples
javascriptregexdry

How to reuse common pattern in multiple regex?


Is it possible to write such instruction by writing only once 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]
}

Solution

  • Write them as string and then use new RegExp(_STRING_); see this