我只希望我的输入只接受字母和空格,因为我的输入服务于名称,这不起作用我已经尝试在正则表达式中放置一些空间。 我的正则表达式正在工作,但空间不是。Javascript正则表达式 - 不接受空间,但它应该
// 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, '');
});
空格是一个非单词字符,所以使用\ W例如 – moped
给我们一个字符串的例子,你想匹配你的正则表达式,你可以看到,有几个正确的答案,来自未知原因不适用于你 – moped
多数民众赞成的事情,theres很多正确的答案,我不知道什么是问题了,为什么它没有为我工作这就是为什么我来到这里。我只需要我的输入即可接受字母和空格,包括ñÑ。 –