This will extract number
var subject = "This is a test message [REF: 2323232]"; var onlyNum = subject.replace(/[^\d]*.*(:\s)(\d*)\]$/g,'''$2'); console.log(onlyNum) Here, same but the number is now a real int
var subject = "This is a test message [REF: 2323232]"; var onlyNum = parseInt(subject.replace(/[^\d]*.*(:\s)(\d*)\]$/g,'''$2')); console.log(onlyNum)