admin管理员组文章数量:1384317
I'm trying to write a regular expression to remove white spaces from just the beginning of the word, not after, and only a single space after the word.
Used RegExp:
var re = new RegExp(/^([a-zA-Z0-9]+\s?)*$/);
Test Exapmle:
1) wordX[space] - Should be allowed
2) [space] - Should not be allowed
3) WrodX[space][space]wordX - Should be allowed
4) WrodX[space][space][space]wordX - Should be allowed
5) WrodX[space][space][space][space] - Should be not be allowed
6) WrodX[space][space] - Allowed with only one space the moment another space is entered **should not be allowed**
I'm trying to write a regular expression to remove white spaces from just the beginning of the word, not after, and only a single space after the word.
Used RegExp:
var re = new RegExp(/^([a-zA-Z0-9]+\s?)*$/);
Test Exapmle:
1) wordX[space] - Should be allowed
2) [space] - Should not be allowed
3) WrodX[space][space]wordX - Should be allowed
4) WrodX[space][space][space]wordX - Should be allowed
5) WrodX[space][space][space][space] - Should be not be allowed
6) WrodX[space][space] - Allowed with only one space the moment another space is entered **should not be allowed**
Share
Improve this question
edited May 26, 2013 at 5:40
Soarabh
asked May 26, 2013 at 5:31
SoarabhSoarabh
2,9609 gold badges41 silver badges57 bronze badges
6
- 2 Your title doesn't seem to match your question. – Barmar Commented May 26, 2013 at 5:37
- Allowed? I can answer the question, but the examples make no sense. – Eli Commented May 26, 2013 at 5:37
- Yeh I know but I have to do something like this. – Soarabh Commented May 26, 2013 at 5:39
-
What language is this for? Why not search for whitespace only:
/\s{2,}/
. In Javascript, you'd callstr.replace(/\s{2,}/, " ")
. – Eric Jablow Commented May 26, 2013 at 5:40 -
Yes, turn around the logic and reject if you see a match on
/^ |[ ][ ]/
. – tripleee Commented May 26, 2013 at 5:46
4 Answers
Reset to default 3Try this one out:
^\s*\w+(\s?$|\s{2,}\w+)+
Test cases ("s added for clarity):
"word" - allowed (match==true)
"word " - allowed (match==true)
"word word" - allowed (match==true)
"word word" - allowed (match==true)
" " - not allowed (match==false)
"word " - not allowed (match==false)
"word " - not allowed (match==false)
" word" - allowed (match==true)
" word" - allowed (match==true)
" word " - allowed (match==true)
" word word" - allowed (match==true)
See demo here.
try to use code that i am giving you and implement with javascript, i hope it will for you fine HTML Code
<input type="test" class="name" />
Javascript Code:
$('.name').keyup(function() {
var $th = $(this);
$th.val($th.val().replace(/(\s{2,})|[^a-zA-Z']/g, ' '));
$th.val($th.val().replace(/^\s*/, ''));
});
This code does not allow more than one spaces between characters or words. Check here JsFiddle Link.
Try this:
var re = /\S\s?$/;
This matches a non-space followed by at most one space at the end of the string.
BTW, there's no need to use new RegExp
when you're providing a regexp literal. That's only needed when converting a string to a RegExp.
Try this regex
/^(\w+)(\s+)/
and your code:
result = inputString.replace(/^(\w+)(\s+)?/g, "$1");
本文标签: javascriptRegExp which allow only one space in between wordsStack Overflow
版权声明:本文标题:javascript - RegExp which allow only one space in between words - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744440007a2606379.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论