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 regex "replace([ -_]g)" deletes numbers? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript regex "replace([ -_]g)" deletes numbers? - Stack Overflow

programmeradmin2浏览0评论

I was doing some tests in Javascript with the replace javascript function.

Consider the following examples executed on a node REPL.
It's a replace that deletes spaces, hyphens and underscores from a string.

> "call this 9344 5 66 22".replace(/[ _-]/g, '');
'callthis934456622'

That was what I was expecting. To only delete the spaces.

However take a look at this:

> "call this 9344 5 66 22".replace(/[ -_]/g, '');
'callthis'

Why when I put this regex bination exact like this -_ (space, hyphen, underscore) it deletes the numbers in the string?


More tests I did:

-(space, hyphen) does not deletes numbers

_(space, underscore) does not deletes numbers

_-(space, underscore, hyphen) does not deletes numbers

-_(hyphen, underscore, space) does not deletes numbers

_-(underscore, hyphen, space) REPL blocks??

-_(space, hyphen, underscore) does deletes numbers

I was doing some tests in Javascript with the replace javascript function.

Consider the following examples executed on a node REPL.
It's a replace that deletes spaces, hyphens and underscores from a string.

> "call this 9344 5 66 22".replace(/[ _-]/g, '');
'callthis934456622'

That was what I was expecting. To only delete the spaces.

However take a look at this:

> "call this 9344 5 66 22".replace(/[ -_]/g, '');
'callthis'

Why when I put this regex bination exact like this -_ (space, hyphen, underscore) it deletes the numbers in the string?


More tests I did:

-(space, hyphen) does not deletes numbers

_(space, underscore) does not deletes numbers

_-(space, underscore, hyphen) does not deletes numbers

-_(hyphen, underscore, space) does not deletes numbers

_-(underscore, hyphen, space) REPL blocks??

-_(space, hyphen, underscore) does deletes numbers

Share Improve this question edited Feb 13, 2014 at 22:42 Tomás asked Jan 24, 2014 at 16:19 TomásTomás 3,5613 gold badges23 silver badges39 bronze badges 0
Add a ment  | 

5 Answers 5

Reset to default 14

[ -_] means characters from space (ASCII 32) to _ (ASCII 95) which includes, among other things, numbers and capital letters.

What you are looking for is [ \-_]. Escaping the - will make it act like the character instead of the meta-character for ranges.

Hyphen if not present at start or end position in a character class needs to be escaped otherwise it represents a range.

So this regex:

[ -_]

will match anything from space to underscore i.e. ASCII 32-95

The - character has special meaning in character classes. When it appears between two characters, it represents a character range — e.g. [a-z] matches any character with a character code between a and z, inclusive.

However, as you've observed, when it's placed at the beginning or end of the character class, it just represents a literal - character. This can also be acplished by escaping the - within the character class — i.e. [ \-_].

"call this 9344 5 66 22".replace(/(\s|-|_)/g, '');

In a class, the dash - character has special meaning as a range operator ONLY when
it doesn't separate clauses, parsed left to right.

Otherwise it is considered no different than any other literal.

Regular expression parsers have no time to worry about good form.

So you can put the dash anywhere you want as a literal, as long as it separates clauses (i.e. its not ambigous).

Most people put it at the end or beginning or escape it so no conceptual errors occur.

Example of clauses, which are hilighted, and literal dashes:

[-a-z-\p{L}-0-9-\x00-\x09-\x20-]

发布评论

评论列表(0)

  1. 暂无评论