gpt4 book ai didi

Javascript Regexp - 不接受空格但它应该

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

我只希望我的输入只接受字母和空格,因为我的输入用于名称,这不起作用我已经尝试在正则表达式中放置一些空格。我的正则表达式有效,但空格无效。

// without space
$('#name').on('input', function (e) {
this.value = this.value.replace(/[^a-zA-Z.,ñÑ ]/g, '');
});

// with space but it didnt work
$('#name').on('input', function (e) {
this.value = this.value.replace(/[^a-zA-Z .,ñÑ]/g, '');
});

// with space again but it fails work also
$('#name').on('input', function (e) {
this.value = this.value.replace(/[^a-zA-Z. ,ñÑ]/g, '');
});

最佳答案

使用 \s 表示单个空格。另外 \s+ 用于匹配多个空格

关于Javascript Regexp - 不接受空格但它应该,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37245570/

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