如何检查数组中的单词的多个值
How to check a word in an array for several values
我的标题可能有点混乱,抱歉。不知道该说什么。
我有一个工作示例,我试图缩短它,但我不知道怎么做。
function pigIt(str){
let words = str.split(" ")
let result = []
for (let word of words) {
if (word === "!" ){
result.push(word)
}
else if (word === "?" ){
result.push(word)
}
else if (word === "." ){
result.push(word)
}
else if (word === "," ){
result.push(word)
}
else if (word.indexOf("?") > -1){
result.push(word.slice(1, word.length-1) + word[0] + "ay" +word[word.length-1])}
else if (word.indexOf("!") > -1){
result.push(word.slice(1, word.length-1) + word[0] + "ay" +word[word.length-1])}
else if (word.indexOf(",") > -1){
result.push(word.slice(1, word.length-1) + word[0] + "ay" +word[word.length-1])}
else if (word.indexOf(".") > -1){
result.push(word.slice(1, word.length-1) + word[0] + "ay" +word[word.length-1])}
else result.push(word.slice(1,word.length) + word[0] + "ay")
}
return result.join(" ")
}
console.log(pigIt("Hello world")) // returns elloHay orldway
console.log(pigIt("Hello world !")) // returns elloHay orldway!
console.log(pigIt("Hello world, what a beautiful day.")) // returns elloHay orldway, hatway aay eautifulbay ayday.
console.log(pigIt("How are you? I am doing good!")) // returns owHay reaay ouyay? Iay maay oingday oodgay!
在这个例子中,如果是“!”、“?”、“、”或“.”,我该如何检查一行?包含在单词中?
下面的 indexOf 也有同样的问题。
谢谢!
使用.match
并使用正则表达式:
if (word.match(/[!?,.]/) {
result.push(word)
}
尝试使用Regex
过滤掉下面的特殊字符
const input = [
"Hello world", // returns elloHay orldway
"Hello world !", // returns elloHay orldway !
"Hello world, what a beautiful day.", // returns elloHay orldway, hatway aay eautifulbay ayday.
"How are you? I am doing good!" // // returns owHay reaay ouyay? Iay maay oingday oodgay!
];
const result = input.map(item => item.split(/\s+/).map(word => {
const matchedVal = word.match(/[,.!?]$/g);
return matchedVal ?
word.length === 1 ? word :
`${word.substring(1, word.length - 1)}${word.charAt(0)}ay${matchedVal[0]}` : `${word.substring(1)}${word.charAt(0)}ay`
}).join(" "));
console.log(result);
尝试使用 some
和 includes
方法
function pigIt(str) {
const words = str.split(" ");
const result = [];
for (let word of words) {
if (["!", "?", ".", ","].some((char) => word.includes(char))) {
word.length > 1
? result.push(
word.slice(1, word.length - 1) +
word[0] +
"ay" +
word[word.length - 1]
)
: result.push(word);
} else {
result.push(word.slice(1) + word[0] + "ay");
}
}
return result.join(" ");
}
console.log(pigIt("Hello world")); // returns elloHay orldway
console.log(pigIt("Hello world !")); // returns elloHay orldway!
console.log(pigIt("Hello world, what a beautiful day.")); // returns elloHay orldway, hatway aay eautifulbay ayday.
console.log(pigIt("How are you? I am doing good!")); // returns owHay reaay ouyay? Iay maay oingday oodgay!
我的标题可能有点混乱,抱歉。不知道该说什么。
我有一个工作示例,我试图缩短它,但我不知道怎么做。
function pigIt(str){
let words = str.split(" ")
let result = []
for (let word of words) {
if (word === "!" ){
result.push(word)
}
else if (word === "?" ){
result.push(word)
}
else if (word === "." ){
result.push(word)
}
else if (word === "," ){
result.push(word)
}
else if (word.indexOf("?") > -1){
result.push(word.slice(1, word.length-1) + word[0] + "ay" +word[word.length-1])}
else if (word.indexOf("!") > -1){
result.push(word.slice(1, word.length-1) + word[0] + "ay" +word[word.length-1])}
else if (word.indexOf(",") > -1){
result.push(word.slice(1, word.length-1) + word[0] + "ay" +word[word.length-1])}
else if (word.indexOf(".") > -1){
result.push(word.slice(1, word.length-1) + word[0] + "ay" +word[word.length-1])}
else result.push(word.slice(1,word.length) + word[0] + "ay")
}
return result.join(" ")
}
console.log(pigIt("Hello world")) // returns elloHay orldway
console.log(pigIt("Hello world !")) // returns elloHay orldway!
console.log(pigIt("Hello world, what a beautiful day.")) // returns elloHay orldway, hatway aay eautifulbay ayday.
console.log(pigIt("How are you? I am doing good!")) // returns owHay reaay ouyay? Iay maay oingday oodgay!
在这个例子中,如果是“!”、“?”、“、”或“.”,我该如何检查一行?包含在单词中? 下面的 indexOf 也有同样的问题。
谢谢!
使用.match
并使用正则表达式:
if (word.match(/[!?,.]/) {
result.push(word)
}
尝试使用Regex
过滤掉下面的特殊字符
const input = [
"Hello world", // returns elloHay orldway
"Hello world !", // returns elloHay orldway !
"Hello world, what a beautiful day.", // returns elloHay orldway, hatway aay eautifulbay ayday.
"How are you? I am doing good!" // // returns owHay reaay ouyay? Iay maay oingday oodgay!
];
const result = input.map(item => item.split(/\s+/).map(word => {
const matchedVal = word.match(/[,.!?]$/g);
return matchedVal ?
word.length === 1 ? word :
`${word.substring(1, word.length - 1)}${word.charAt(0)}ay${matchedVal[0]}` : `${word.substring(1)}${word.charAt(0)}ay`
}).join(" "));
console.log(result);
尝试使用 some
和 includes
方法
function pigIt(str) {
const words = str.split(" ");
const result = [];
for (let word of words) {
if (["!", "?", ".", ","].some((char) => word.includes(char))) {
word.length > 1
? result.push(
word.slice(1, word.length - 1) +
word[0] +
"ay" +
word[word.length - 1]
)
: result.push(word);
} else {
result.push(word.slice(1) + word[0] + "ay");
}
}
return result.join(" ");
}
console.log(pigIt("Hello world")); // returns elloHay orldway
console.log(pigIt("Hello world !")); // returns elloHay orldway!
console.log(pigIt("Hello world, what a beautiful day.")); // returns elloHay orldway, hatway aay eautifulbay ayday.
console.log(pigIt("How are you? I am doing good!")); // returns owHay reaay ouyay? Iay maay oingday oodgay!