You could split
each string to words and match the same words by using filter
function getWords(str) {
return str.split(" ").filter(Boolean);
}
function getMatchedWords(words1, words2) {
return words1.filter((word) => words2.includes(word));
}
const string1 = "The lazy fox jumps over the fence";
const string2 = "The lazy dog jumps under a fence yesterday";
const words1 = getWords(string1);
const words2 = getWords(string2);
const matchedWords = getMatchedWords(words1, words2);
const ratio = +((100 * matchedWords.length) / words1.length).toPrecision(2);
console.log(ratio);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…