return FALSE; $r = well_tag_thread__update(array('id' => $id), $update); return $r; } function well_tag_thread_find($tagid, $page, $pagesize) { $arr = well_tag_thread__find(array('tagid' => $tagid), array('id' => -1), $page, $pagesize); return $arr; } function well_tag_thread_find_by_tid($tid, $page, $pagesize) { $arr = well_tag_thread__find(array('tid' => $tid), array(), $page, $pagesize); return $arr; } ?>javascript - RegExp which allow only one space in between words - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - RegExp which allow only one space in between words - Stack Overflow

programmeradmin5浏览0评论

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 call str.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
 |  Show 1 more ment

4 Answers 4

Reset to default 3

Try 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");
发布评论

评论列表(0)

  1. 暂无评论