Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/424.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript regexp在单词之间只允许一个空格_Javascript_Regex - Fatal编程技术网

Javascript regexp在单词之间只允许一个空格

Javascript regexp在单词之间只允许一个空格,javascript,regex,Javascript,Regex,我正在尝试编写一个正则表达式来删除单词开头的空格,而不是后面的空格,并且只删除单词后面的单个空格 使用的RegExp: var re = new RegExp(/^([a-zA-Z0-9]+\s?)*$/); 测试示例: 1) test[space]ing - Should be allowed 2) testing - Should be allowed 3) [space]testing - Should not be allowed 4) testing[space] - Shou

我正在尝试编写一个正则表达式来删除单词开头的空格,而不是后面的空格,并且只删除单词后面的单个空格

使用的RegExp:

var re = new RegExp(/^([a-zA-Z0-9]+\s?)*$/);
测试示例:

1) test[space]ing - Should be allowed 
2) testing - Should be allowed 
3) [space]testing - Should not be allowed 
4) testing[space] - Should be allowed but have to trim it 
5) testing[space][space] - should be allowed but have to trim it 
只允许有一个空间。可能吗

function validate(s) {
    if (/^(\w+\s?)*\s*$/.test(s)) {
        return s.replace(/\s+$/, '');
    }
    return 'NOT ALLOWED';
}
validate('test ing')    // => 'test ing'
validate('testing')     // => 'testing'
validate(' testing')    // => 'NOT ALLOWED'
validate('testing ')    // => 'testing'
validate('testing  ')   // => 'testing'
validate('test ing  ')  // => 'test ing'

顺便说一句,如果您使用正则表达式文本,
newregexp(..)
是多余的。

要匹配您需要的内容,您可以使用

var re = /^([a-zA-Z0-9]+\s)*[a-zA-Z0-9]+$/;
也许你可以把它缩短一点,但它也与
\uu
匹配

var re = /^(\w+\s)*\w+$/;

这不允许前后空格加上单词之间的空格。您可以随意添加任何您想要的特殊字符

^([A-Za-z]+ )+[A-Za-z]+$|^[A-Za-z]+$

这是一个没有正则表达式的解决方案。 将此脚本添加到document.ready函数中即可

var i=0;
        jQuery("input,textarea").on('keypress',function(e){
            //alert();
            if(jQuery(this).val().length < 1){
                if(e.which == 32){
                    //alert(e.which);
                return false;
                }   
            }
            else {
            if(e.which == 32){
                if(i != 0){
                return false;
                }
                i++;
            }
            else{
                i=0;
            }
            }
        });
var i=0;
jQuery(“输入,文本区域”)。打开('keypress',函数(e){
//警惕();
if(jQuery(this).val().length<1){
如果(e.which==32){
//警报(例如,哪个);
返回false;
}   
}
否则{
如果(e.which==32){
如果(i!=0){
返回false;
}
i++;
}
否则{
i=0;
}
}
});

工作代码-在我的
名称中。addTextChangedListener()

您可以尝试根据您的代码调整此选项。

使用此选项

^([A-Za-z]{5,}|[\s]{1}[A-Za-z]{1,})*$

演示:-

您正在尝试匹配或替换?@sp00m我只是在尝试匹配。@MichalBrašna我想允许用户在两者之间有一个空格。是否应匹配
a b c
?我更喜欢此解决方案,因为它还允许单字母匹配。@falsetru这是OP的重点
([a-zA-Z0-9]+\s])*
inregex@coderman,我更新了答案。前面的答案只匹配两个字符串。对于有效的“测试”输入,它返回false。@coderman,对于我,它返回
true
。也许您使用的是较旧的答案?@coderman ya应该是
测试[space]ing
,这样您的答案就可以了。@Ranjit,删除
^
$
。(斜杠是javascript中正则表达式文字的一部分,也可以删除它。)并将
\
替换为
\\
。欲了解更多信息,请发布一个单独的问题。它是否有效?这看起来缺少一个右括号。欢迎使用堆栈溢出!请阅读。为什么我或者其他人要“用这个”?与其添加这样的非句子,不如用问题来解释为什么这个正则表达式做OP想要做的事情。永远记住,你不仅是在回答OP,而且是在回答任何未来的读者(比如你自己,因为这个问题已经6岁了)。愚蠢的
{1}
是一个绝对的赠品,这是一个纯粹的正则表达式初学者。
public void onTextChanged(CharSequence s, int start, int before, int count) {
            String n = name.getText().toString();
            if (n.equals(""))
                name.setError("Name required");
            else if (!n.matches("[\\p{Alpha}\\s]*\\b") | n.matches(".*\\s{2}.*") | n.matches("\\s.*")) {
                if (n.matches("\\s.*"))
                    name.setError("Name cannot begin with a space");
                else if (n.matches(".*\\s{2}.*"))
                    name.setError("Multiple spaces between texts");
                else if (n.matches(".*\\s"))
                    name.setError("Blank space at the end of text");
                else
                    name.setError("Non-alphabetic character entered");
            }
        }
^([A-Za-z]{5,}|[\s]{1}[A-Za-z]{1,})*$