gpt4 book ai didi

JavaScript:如何从字符串中删除任何包含(或直接位于其前面)大写字母、数字或逗号的单词?

转载 作者:行者123 更新时间:2023-11-28 16:06:44 27 4
gpt4 key购买 nike

我正在尝试编写代码,以便从字符串(文本)中删除“坏”单词。

如果该词后面有逗号或任何特殊符号,则该词是“坏”的。如果该单词仅包含 a 到 z(小写字母),则该单词并不“坏”。

所以,我想要达到的结果是:

<script>
String.prototype.azwords = function() {
return this.replace(/[^a-z]+/g, "0");
}

var res = "good Remove remove1 remove, ### rem0ve? RemoVE gooood remove.".azwords();//should be "good gooood"
//Remove has a capital letter
//remove1 has 1
//remove, has comma
//### has three #
//rem0ve? has 0 and ?
//RemoVE has R and V and E
//remove. has .
alert(res);//should alert "good gooood"
</script>

最佳答案

试试这个:

return this.replace(/(^|\s+)[a-z]*[^a-z\s]\S*(?!\S)/g, "");

它尝试匹配一个单词(由空格/字符串结尾包围)并包含任何(非空格)字符,但至少有一个不是 a-z。然而,这是相当复杂且难以维护的。也许你应该尝试一种更实用的方法:

return this.split(/\s+/).filter(function(word) {
return word && !/[^a-z]/.test(word);
}).join(" ");

关于JavaScript:如何从字符串中删除任何包含(或直接位于其前面)大写字母、数字或逗号的单词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14624087/

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