Assuming the format you provided us is exact then you can try something like this.
let exampleString = "3 years, 4 Months and 4 days";
const extractYMD = (input) => {
const stringArray = input.split(' ');
//split the string on every space
//it will create an array that looks like this
//["3", "years,", "4", "Months", "and", "4", "days"]
//then you can use the index to find your ints
const years = parseInt(stringArray[0])
const months = parseInt(stringArray[2])
const days = parseInt(stringArray[5])
//using parseInt because values inside the array are still strings
//don't need to assign to variables but did it for clarity
if(years <= 3){
console.log('example condition')
//do something here
}
console.log(years, months, days)
//logging to console so you can see output
return [years, months, days];
// return the values if you need them for something
};
extractYMD(exampleString);
Using regex as others have suggested is also an option but there is a risk you will get an output like 324 and you won't know if its 32 years and 4 days or 3 months and 24 days. You can learn about and test regex here.
Keep in mind that the function above is very dependent on the format being exactly how you described. Any deviation will cause problems. Ideally you should seek to retrieve the data before it gets converted to this string format. But we need to see more of your code to understand why you're in this situation.