如何通过js从文本中删除洪水

How to remove flooding from text via js?

本文关键字:删除 文本 何通过 js      更新时间:2023-09-26

我不知道我想要的东西的名字,所以我给你看看。

我有一个输入文本,用户可以问问题,但有一些用户问这样的问题:What the?????????????????????%%%%%%Question?????????????????????? ....或!!!!!!?????? ....等。

我想从我的javascript变量中删除这些字符串。哪个函数对我有帮助?我要找的东西的英文名是什么?

谢谢。

JavaScript中的示例,但regexp在其他语言中非常相似:

var compressSpecialChars = function(str) {
    return str.replace(/[^'w'd's]+/g, function(specials) { return specials[0] });
};
var questions = [
    'What the?????????????????????%%%%%%',
    'Question??????????????????????',
    '!!!!!!??????'
];
questions.map(compressSpecialChars);
// Result: ["What the?", "Question?", "!"]