Trying to create a forgiving regex for email address parsing, and so far I have
var regex = /(?:"?([A-Z][^<"]+)"?\s*)?<?([^>\s,]+)/gi;
var ip='"Bob Smith" <[email protected]>, <[email protected]>, "John Doe <[email protected]>, foo Bar <[email protected]>, <[email protected]>, Ln, Fn<[email protected]>,"Mn, Gn"<[email protected]>,Nn, Hn <[email protected]>, [email protected], [email protected], In,On <[email protected]>';
while (m = regex.exec(ip)) {
if (m[1]) {
m[1] = m[1].trim()
}
extracts.push({
name: m[1]?m[1]:m[2].replace(/@.*/,""),
email: m[2]
});
which matches the following,
"Bob Smith" <[email protected]>, <[email protected]>, "John Doe <[email protected]>, foo Bar <[email protected]>, <[email protected]>, Ln, Fn<[email protected]>,"Mn, Gn"<[email protected]>,Nn, Hn <[email protected]>
but misses these,
[email protected], [email protected], In,On <[email protected]>
The code
function paintExtracts(extracts) {
var html = '<table border="1"><thead><tr><td>Name</td><td>Email</td></tr></thead><tbody>';
for (var i = 0; i < extracts.length; i++) {
html += '<tr><td>"' + extracts[i].name + '"</td><td>"' + extracts[i].email + '"</td></tr>';
}
html += '</tbody></table>';
$('#opdiv').html('').append(html);
}
var regex = /(?:"?([A-Z][^<"]+)"?\s*)?<?([^>\s,]+)/gi
$('#parsebtn').click(function() {
var extracts = [];
var ip = $('#inputta').val();
while (m = regex.exec(ip)) {
if (m[1]) {
m[1] = m[1].trim()
}
debugger;
extracts.push({
name: m[1]?m[1]:m[2].replace(/@.*/,""),
email: m[2]
});
}
paintExtracts(extracts);
});
table {
font-family: monospace;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea id="inputta" rows="12" cols="65">"Bob Smith" <[email protected]>, <[email protected]>, "John Doe <[email protected]>
foo Bar <[email protected]>, <[email protected]>,
</textarea>
<p>
<input type="button" id='parsebtn' value="Parse"></input>
<div id="opdiv">
<h1>
Parsed Data
</h1>
</div>