te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - Regular expression to match only character or space or one dot between two words, no double space allowed - Stack O
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Regular expression to match only character or space or one dot between two words, no double space allowed - Stack O

programmeradmin3浏览0评论

I need help with regular expression.I need a expression in JavaScript which allows only character or space or one dot between two words, no double space allowed.

I am using this

var regexp = /^([a-zA-Z]+\s)*[a-zA-Z]+$/;

but it's not working.

Example

1.  hello space .hello - not allowed
2.  space hello space - not allowed

I need help with regular expression.I need a expression in JavaScript which allows only character or space or one dot between two words, no double space allowed.

I am using this

var regexp = /^([a-zA-Z]+\s)*[a-zA-Z]+$/;

but it's not working.

Example

1.  hello space .hello - not allowed
2.  space hello space - not allowed
Share Improve this question edited Jun 3, 2014 at 6:43 Deele 3,6842 gold badges35 silver badges52 bronze badges asked Jun 3, 2014 at 5:06 user3291547user3291547 731 gold badge1 silver badge4 bronze badges 2
  • $ - means end of string, also no dash in regex. what actually you need to get? – Subdigger Commented Jun 3, 2014 at 5:12
  • Ending space, also no dash Example testing.t not allowed – user3291547 Commented Jun 3, 2014 at 5:14
Add a ment  | 

4 Answers 4

Reset to default 7

try this:

^(\s?\.?[a-zA-Z]+)+$

EDIT1

/^(\s{0,1}\.{0,1}[a-zA-Z]+)+$/.test('space ..hello space')
false
/^(\s{0,1}\.{0,1}[a-zA-Z]+)+$/.test('space .hello space')
true

v2:

/^(\s?\.?[a-zA-Z]+)+$/.test('space .hello space')
true
/^(\s?\.?[a-zA-Z]+)+$/.test('space ..hello space')
false

v3: if you need some thisn like one space or dot between

/^([\s\.]?[a-zA-Z]+)+$/.test('space hello space')
true
/^([\s\.]?[a-zA-Z]+)+$/.test('space.hello space')
true
/^([\s\.]?[a-zA-Z]+)+$/.test('space .hello space')
false

v4:

/^([ \.]?[a-zA-Z]+)+$/.test('space hello space')
true
/^([ \.]?[a-zA-Z]+)+$/.test('space.hello space')
true
/^([ \.]?[a-zA-Z]+)+$/.test('space .hello space')
false
/^([ ]?\.?[a-zA-Z]+)+$/.test('space .hello space')
true

EDIT2 Explanation:

may be problem in \s = [\r\n\t\f ] so if only space allowed - \s? can be replaced with [ ]?

http://regex101./r/wV4yY5

This regular expression will match multiple spaces or dots between words and spaces before the first word or after the last word. This is the opposite of what you want, but you can always invert it (!foo.match(...)):

/\b[\. ]{2,}\b|^ | $/

In regex101.: http://regex101./r/fT0pF2

And in plainer english:

\b        => a word boundary
[\. ]     => a dot or a space
{2,}      => 2 or more of the preceding
\b        => another word boundary
|         => OR
^{space}  => space after string start
|         => OR
{space}$  =>  space before string end

This will match:

"this  that" // <= has two spaces
"this. that" // <= has dot space
" this that" // <= has space before first word
"this that " // <= has space after last word

But it will not match:

"this.that and the other thing"

How about that?

^\s*([a-zA-Z]+\s?\.?[a-zA-Z]+)+$

This allows:

  1. Multiple leading spaces.
  2. Space as a delimiter.
  3. Dot in the second and any consecutive word.

Try this (only a single whitespace or a period is allowed between the words):

> /\w+[\s\.]\w+/.test('foo bar')
true
> /\w+[\s\.]\w+/.test('foo.bar')
true
> /\w+[\s\.]\w+/.test('foo..bar')
false
> /\w+[\s\.]\w+/.test('foo .bar')
false

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论