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; } ?>regex - What does this JavaScript Regular Expression [^d.-] mean? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

regex - What does this JavaScript Regular Expression [^d.-] mean? - Stack Overflow

programmeradmin3浏览0评论

We had a developer here who had added following line of code to a web application:

var amount = newValue.replace(/[^\d.-]/g, '');

The particular line deals with amount values that a user may enter into a field.

I know the following about the regular expression:

  1. that it replaces the matches with empty strings (i.e. removes them)
  2. that /g is a flag that means to match all occurrences inside "newValue"
  3. that the brackets [] denote a special group
  4. that ^ means beginning of the line
  5. that d means digits

Unfortunately I do not know enough to determine what kind of strings this should match. I checked with some web-based regex testers if it matches e.g. strings like 98.- and other alternatives with numbers but so far no luck.

My problem is that it seems to make IE very slow so I need to replace it with something else.

Any help on this would be appreciated.

Edit: Thanks to all who replied. I tried not just Google but sites like myregextester, regular-expressions.info, phpliveregex, and others. My problem was misunderstanding the meaning of ^ and expecting that this required a numeric string like 44.99.

We had a developer here who had added following line of code to a web application:

var amount = newValue.replace(/[^\d.-]/g, '');

The particular line deals with amount values that a user may enter into a field.

I know the following about the regular expression:

  1. that it replaces the matches with empty strings (i.e. removes them)
  2. that /g is a flag that means to match all occurrences inside "newValue"
  3. that the brackets [] denote a special group
  4. that ^ means beginning of the line
  5. that d means digits

Unfortunately I do not know enough to determine what kind of strings this should match. I checked with some web-based regex testers if it matches e.g. strings like 98.- and other alternatives with numbers but so far no luck.

My problem is that it seems to make IE very slow so I need to replace it with something else.

Any help on this would be appreciated.

Edit: Thanks to all who replied. I tried not just Google but sites like myregextester., regular-expressions.info, phpliveregex., and others. My problem was misunderstanding the meaning of ^ and expecting that this required a numeric string like 44.99.

Share Improve this question edited Oct 25, 2013 at 0:20 user100487 asked Oct 24, 2013 at 22:16 user100487user100487 2581 gold badge6 silver badges15 bronze badges 4
  • 1 Just try it... I don't see why you need to ask anyone. Pump some strings into the .replace and see what you get. Spoiler Alert it returns only digits, periods, and hyphens... You can even open a JS console in your browser and test by running something like: 'abcABC123$%^.--_'.replace(/[^\d.-]/g, ''); – Jasper Commented Oct 24, 2013 at 22:20
  • @SLaks: 2 is correct. No? – Blue Skies Commented Oct 24, 2013 at 22:23
  • Might be helpful for future reference. Take a look at the description from here regex101./r/rV6mY0 – javaCity Commented Oct 24, 2013 at 22:45
  • @BlueSkies: He changed the numbering (Or I made a typo). I meant what is now 3. – SLaks Commented Oct 25, 2013 at 0:14
Add a ment  | 

2 Answers 2

Reset to default 13

Inside the group, when the ^ is the first character, it works as a negation of the character matches. In other words, it's saying match any character that are not the ones in the group.

So this will mean "match anything that is not a digit, a period, or a hyphen".

The ^ character is a negation character.

var newValue = " x44x.-x ";
var amount = newValue.replace(/[^\d.-]/g, '');
console.log(amount);

will print

44.-

I suspect the developer maybe just wanted to remove trailing whitespaces? I would rather try to parse the string for numbers and remove anything else.

发布评论

评论列表(0)

  1. 暂无评论