gpt4 book ai didi

javascript - 从字符串javascript中删除字符的更简单方法

转载 作者:行者123 更新时间:2023-11-30 10:58:11 25 4
gpt4 key购买 nike

我有一组评论,我想生成一个字符串,除了 a-z' 之外的所有字符都被删除

EDIT::我实际上希望删除除 a-z' 之外的所有字符,只保留单词之间的一个空格,并将所有大写字母小写

这是我当前的代码,可以正常工作,

let words = ["A great product for daily use",
"Great price. Takes about 10days for delivery",
"Excellent value - will buy again",
"Fine, I always use this, was as expected",
"Good value",
"excellent product, good value",
"good",
"does the job!",
"Thank you",
"Great it's that is easy to use",
"I hated it",
"arrived on time, excellent product, thank you",
"quick service great price.",
"good and refreshing",
"My daughter is road testing this, but so far it's refreshing",
"DO NOT BUY THIS PRODUCT",
"Avoid",
"Did not notice any difference",
"Horrible taste",
"Does its job and shows it works"]

joinedwords = words.join(' ')
removeChars = joinedwords.replace(/[^\w]/gi, " ").toLowerCase()
replaceApos = removeChars.replace(/it\ss\s/g, "it's ")
replaceNum = replaceApos.replace(/[0-9]/g, "")
replaceWhi = replaceNum.replace(/\s\s+/g, " ")

但是有人可以建议更好/更有效/更简单的修复方法吗?

如果正则表达式需要不同的输出,能否将它们链接成一个?

谢谢

最佳答案

let words = ["A great product for daily use",
"Great price. Takes about 10days for delivery",
"Excellent value - will buy again",
"Fine, I always use this, was as expected",
"Good value",
"excellent product, good value",
"good",
"does the job!",
"Thank you",
"Great it's that is easy to use",
"I hated it",
"arrived on time, excellent product, thank you",
"quick service great price.",
"good and refreshing",
"My daughter is road testing this, but so far it's refreshing",
"DO NOT BUY THIS PRODUCT",
"Avoid",
"Did not notice any difference",
"Horrible taste",
"Does its job and shows it works"];

joinedwords = words.join(' ');
removeChars = joinedwords.replace(/[^A-Za-z' ]/g, "").toLowerCase();

//Prints result
document.write(removeChars);

多多。我运行了你的代码,也运行了我的代码,它们似乎共享相同的结果,所以我猜这就是你想要的?顺便说一下,正则表达式是这样工作的,它匹配任何不是字母表、空格或撇号的字符。

joinedwords = words.join(' ')
removeChars = joinedwords.replace(/[^A-Za-z' ]/g, "").toLowerCase()

关于javascript - 从字符串javascript中删除字符的更简单方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59140403/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com