“/^ $/”的这些运算符是什么?在这种情况下它是如何工作的?

What are these operators for "/^ $/" and how does it work in this case?

最近我在 JS 中进行代码战挑战,当您提交代码时,他们会向您展示其他人如何完成挑战,这是解决方案之一。当我浏览它时,我注意到了这些标记,我想了解它们并学习如何使用它们。好像有个“?”在中间也导致相信它与三元运算符有关。

let data = [":~)", ";->", ";~(", ":~D", ";o>", ":)", ";oD"];

function countSmileys(data) {
    return data.filter(x => /^[:;][-~]?[)D]$/.test(x)).length;   
}

我可能对它的功能有一些模糊的想法,因为我知道输出应该是什么。
感谢您的宝贵时间!

他们使用的是regular expresions

与三元运算符无关,那是一个正则表达式,该函数试图做的是过滤数组中的元素。

function countSmileys(data) {
    //filter out the elements that don't match the regular expression you want to test
    return data.filter(x => /^[:;][-~]?[)D]$/.test(x)).length;   
}

您可以阅读更多关于正则表达式的内容 here:

test 函数 here