So I need to pull a number value from a string. I currently have a working solution but I feel that maybe I can improve this using a regular expression or something.
Here is my working solution
var subject = "This is a test message [REF: 2323232]"; if(subject.indexOf("[REF: ") > -1){ var startIndex = subject.indexOf("[REF: "); var result = subject.substring(startIndex); var indexOfLastBrace = result.indexOf("]"); var IndexOfRef = result.indexOf("[REF: "); var ticketNumber = result.substring(IndexOfRef + 6, indexOfLastBrace); if(!isNaN(ticketNumber)){ console.log("The ticket number is " + ticketNumber) console.log("Valid ticket number"); } else{ console.log("Invalid ticket number"); } } As you can see I'm trying to pull the number value from after the "[REF: " string.
\[REF: *(\d+)in amatch():subject.match(/\[REF: *(\d+)/)[1]+subject.split("[REF: ")[1].slice(0,-1);