This is a possible solution that includes the omission of special characters, I assume that, as mentioned in the title of the question, the first characters are numbers
The algorithm consists in iterating over the string of numbers and letters, accumulating digits until finding the first letter, returning an object. To check if a value is numerical, I based myself on what was discussed in the following question: how to check whether a value is a number in javascript or jquery
function isNumeric(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
function processInput(input){
let result = {
firstNumber:"",
firstLetter:""
}
const cleanInput = input.replace(/[^\w\s]/gi, ''); // reemplazo todo lo que no sea una letra o un digito. Esto podria no ser necesario si ya se asume que se controla en el ingreso de datos.
console.log("input: %s, clean input: %s", input, cleanInput);
for (let i in cleanInput) {
if(isNumeric(cleanInput[i])){
result.firstNumber += cleanInput[i];
}else{
result.firstLetter = cleanInput[i];
break;
}
}
return result;
}
console.log(processInput("123dkkkk"));
console.log(processInput("23dk"));
console.log(processInput("1d23dkkkk"));
console.log(processInput("#11d23dk"));
console.log(processInput("#1..1??#23dk"));
console.log(processInput("#1..1??#DDD23dk"));