正则表达式在第一个字符后强制单个空格 [英] Regular expression to force single blank space after a first character
本文介绍了正则表达式在第一个字符后强制单个空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在第一个字符后强制空格作为用户类型。我只需要在第一个字符后面只有一个空格(在第一个字符之前不允许空格)。在第一个字符和空格之后最多可以有40个字符。
How do I force a space after first character as user types. I just need only one space after first character (no spaces allowed before first one). After first character and space there can be upto 40 characters.
我发现这个到目前为止,但不符合我的需要
I found this so far, but does not fit my needs
$('.singleSpace').keyup(function() {
var foo = $(this).val().split(" ").join(""); // remove hyphens
if (foo.length > 0) {
foo = foo.match(new RegExp('.{1}', 'g')).join(" ");
}
$(this).val(foo);
});
推荐答案
我不知道你为什么要这样做但要删除你可以使用这个空格:
I don't know why you want to do this but to remove spaces you can use this :
var str = $(this).val().replace(" ", "");
并在第一个字符后添加空格:
And for add a space after first character :
var str = $(this).replace("^(.)(.*)$", "$1 $2");
这篇关于正则表达式在第一个字符后强制单个空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文