I have a text and I want to search in text, find the word ,and then return the numbers after this word
for example :
this is a signal , entry : 2430 and side is short
I want to find entry and return 2430
how can I handle this?
Just use a simple regex:
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]; // undefined
If you want it on 1 line:
text = 'this is a signal , entry : 2430 and side is short';
x = text.split('entry : ')[1].split(' ')[0]; // 2430
You can also do with indexOf and regular expression
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);
}