我有一个简单地STR1(我们参考的字符串)比较STR2(用户输入)的编码,以检查是否在str2中的任何字错误或输入正确。该代码工作正常,但我不能找到一个解决这个问题的:
我想忽略多余的空格,逗号和点和任何其他文字标志两个字符串进行比较。就像一个听写应用...
例如,下面的字符串应该假设上输出为等于字符串:
STR1 =“我被送到地球”
STR2赛车=“我,送:土。”
任何其他的修改以改进该代码极为赏识。请帮忙...
var str1 = "I was sent to earth to protect my cousin";
var str2 = "I waz Sent to earth to protet my cousine";
let a = str1.toLowerCase().split(' ');
let b = str2.toLowerCase().split(' ');
let res1 = b.filter(i => !a.includes(i));
let res2 = a.filter(i => !b.includes(i));
console.log(res1);
console.log(res2);
var str1 = res1.toString();
str1 = str1.replace(/,/g, '\n');
var str2 = res2.toString();
str2 = str2.replace(/,/g, '\n');
var player = GetPlayer();
player.SetVar("wrong_Spell",str1);
player.SetVar("right_Spell",str2);
你可以使用match
使用\w+
提取的话:
function words(s) {
return s.toLowerCase().match(/\w+/g);
}
var str1 = "I was sent to earth to protect my cousin";
var str2 = "I waz Sent to earth to protet my cousine";
let a = words(str1);
let b = words(str2);
let res1 = b.filter(i => !a.includes(i));
let res2 = a.filter(i => !b.includes(i));
console.log(res1);
console.log(res2);