I need to create a JSON object by parsing a search string.
Examples of input strings
"American01 SFO2016"
"American 01 SFO 2016"
"American-01 2016SFO"
"American:01 SFO16"
The expected output for all these strings is
{
airline: "American",
flightNo: 01,
airport: "SFO",
year: "2016"
}
Code snippet
var str = "American-01 2016SFO";
document.write(str.split(/[ :-]+/));
This code will output American,01, 2016SFO
I have two questions: 1) How can I split 2016SFO into 2016 and SFO? 2) Is this the best approach for achieving this?
Here's a demo link https://jsfiddle.net/pdubey84/msebk5a2/