I have this string :
var author = "Tom Smith, Will Hughes, Adonis Young and Tyrek Hill";
And this is the function getNumOfAuthor :
function getNumOfAuthor(m_name:string) {
const regex = /\s*(?:,|$)\s*/;
var str = m_name.split(regex);
alert(str.length);
alert(str);
if (str.length == 1) {
num_of_author = 1;
}
else if (str.length > 1) {
num_of_author = str.length;
}
return num_of_author;
}
I want to use the split() method so that the full names are separated into elements in string array using regex as the split delimiter
Does anyone know what the regex would be? I can get comma working, but I can't seem to figure out how to have multiple punctuation and specific phrases working alongside it
author.replace(/\sand\s/, ', ').split(/(?:,\s?)/)My output from yourauthorvariable is:["Tom Smith", "Will Hughes", "Adonis Young", "Tyrek Hill"]