Tengo un texto y quiero buscar en el texto, encontrar la palabra y luego devolver los números después de esta palabra
por ejemplo :
this is a signal , entry : 2430 and side is shortquiero encontrar entrada y retorno 2430
¿Cómo puedo manejar esto?
Simplemente use una expresión regular simple:
const input = 'this is a signal , entry : 2430 and side is short'; const number1 = input.match(/entry\W+(\d+)/)?.[1]; // "2430" const number2 = input.match(/santa\W+(\d+)/)?.[1]; // undefinedSi lo quieres en 1 línea:
text = 'this is a signal , entry : 2430 and side is short'; x = text.split('entry : ')[1].split(' ')[0]; // 2430También puedes hacerlo con indexOf y expresión regular.
const word = "entry"; const str = "this is a signal , entry : 2430 and side is short"; const index = str.indexOf(word); if (index !== -1) { let result = str.slice(index + word.length).match(/\d+/)[0]; console.log(result); }