gpt4 book ai didi

JavaScript 从字符串中删除句号、逗号和空格

转载 作者:搜寻专家 更新时间:2023-11-01 04:53:51 25 4
gpt4 key购买 nike

我正在尝试从字符串中删除句号、逗号和空格。我很确定我必须使用这个来做到这一点:str.replace(<some Regex here>, "");

我对 Regex 不是很熟悉,我该怎么做?

最佳答案

使用这个正则表达式 /[.,\s]/g

var str = 'abc abc a, .aa ';

var regex = /[.,\s]/g;

var result = str.replace(regex, '');

document.write(result);

除了字符类 [] 中的 ^-]\ 之外,您不需要转义字符

Any character except ^-]\ add that character to the possible matches for the character class.

关于JavaScript 从字符串中删除句号、逗号和空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36817120/

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