解法:
const str = "I am an 20-years out--standing @ * -stu- dent";
function solution(str) {
const arr = str.split(" ");
const newArr = arr.map((str) => {
if (/[a-zA-Z0-9-]/.test(str)) {
if (/-{2}/g.test(str)) {
return str.replace(/--/g, " ");
} else if (str.startsWith("-") || str.endsWith("-")) {
return str.replace(/-/g, "");
} else {
return str;
}
} else return "";
});
console.log("new", newArr);
return newArr
.reverse()
.filter((item) => item !== "")
.join(" ");
}
console.log(solution(str));
/*
I am an 20-years out--standing @ * -stu- dent => dent stu standing out 20-years an am I
*/