Get part of string matching a Regular Expression and then pass its value to a callback
const pickStringByRegex = (pattern, str, func) => {
const regex = new RegExp(pattern)
const value = regex.test(str) ? str.replace(regex, '$2') : null
if (value) {
func(value)
}
}
// Usage:
const pattern = /^(prefix_(V1|V2|V3|V4|V5))$/
const search = 'prefix_V2'
pickStringByRegex(pattern, search, (v) => console.log('Value matched: %s', v))
// > Value matched: V2