权限没有,则隐藏 function forum_list_access_filter($forumlist, $gid, $allow = 'allowread') { global $grouplist; if (empty($forumlist)) return array(); if (1 == $gid) return $forumlist; $forumlist_filter = $forumlist; $group = $grouplist[$gid]; foreach ($forumlist_filter as $fid => $forum) { if (empty($forum['accesson']) && empty($group[$allow]) || !empty($forum['accesson']) && empty($forum['accesslist'][$gid][$allow])) { unset($forumlist_filter[$fid]); } unset($forumlist_filter[$fid]['accesslist']); } return $forumlist_filter; } function forum_filter_moduid($moduids) { $moduids = trim($moduids); if (empty($moduids)) return ''; $arr = explode(',', $moduids); $r = array(); foreach ($arr as $_uid) { $_uid = intval($_uid); $_user = user_read($_uid); if (empty($_user)) continue; if ($_user['gid'] > 4) continue; $r[] = $_uid; } return implode(',', $r); } function forum_safe_info($forum) { //unset($forum['moduids']); return $forum; } function forum_filter($forumlist) { foreach ($forumlist as &$val) { unset($val['brief'], $val['announcement'], $val['seo_title'], $val['seo_keywords'], $val['create_date_fmt'], $val['icon_url'], $val['modlist']); } return $forumlist; } function forum_format_url($forum) { global $conf; if (0 == $forum['category']) { // 列表URL $url = url('list-' . $forum['fid'], '', FALSE); } elseif (1 == $forum['category']) { // 频道 $url = url('category-' . $forum['fid'], '', FALSE); } elseif (2 == $forum['category']) { // 单页 $url = url('read-' . trim($forum['brief']), '', FALSE); } if ($conf['url_rewrite_on'] > 1 && $forum['well_alias']) { if (0 == $forum['category'] || 1 == $forum['category']) { $url = url($forum['well_alias'], '', FALSE); } elseif (2 == $forum['category']) { // 单页 $url = ($forum['threads'] && $forum['brief']) ? url($forum['well_alias'] . '-' . trim($forum['brief']), '', FALSE) : url($forum['well_alias'], '', FALSE); } } return $url; } function well_forum_alias() { $forumlist = forum_list_cache(); if (empty($forumlist)) return ''; $key = 'forum-alias'; static $cache = array(); if (isset($cache[$key])) return $cache[$key]; $cache[$key] = array(); foreach ($forumlist as $val) { if ($val['well_alias']) $cache[$key][$val['fid']] = $val['well_alias']; } return array_flip($cache[$key]); } function well_forum_alias_cache() { global $conf; $key = 'forum-alias-cache'; static $cache = array(); // 用静态变量只能在当前 request 生命周期缓存,跨进程需要再加一层缓存:redis/memcached/xcache/apc if (isset($cache[$key])) return $cache[$key]; if ('mysql' == $conf['cache']['type']) { $arr = well_forum_alias(); } else { $arr = cache_get($key); if (NULL === $arr) { $arr = well_forum_alias(); !empty($arr) AND cache_set($key, $arr); } } $cache[$key] = empty($arr) ? '' : $arr; return $cache[$key]; } ?>javascript - How to replace all matching characters except the first occurrence - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to replace all matching characters except the first occurrence - Stack Overflow

programmeradmin11浏览0评论

I am trying to use regex to pare a string in JavaScript. I want to replace all '.'s and '%'s with empty character '' but the catch is I don't want to replace the first occurrence of '.'.

value.replace(/\%\./g, '');

Expected result like below:

.4.5.6.7. ==> .4567
4.5667.444... ==> 4.56667444
..3445.4 ==> .34454

I am trying to use regex to pare a string in JavaScript. I want to replace all '.'s and '%'s with empty character '' but the catch is I don't want to replace the first occurrence of '.'.

value.replace(/\%\./g, '');

Expected result like below:

.4.5.6.7. ==> .4567
4.5667.444... ==> 4.56667444
..3445.4 ==> .34454
Share Improve this question edited Sep 14, 2016 at 3:20 user663031 asked May 14, 2015 at 0:07 Rahul DessRahul Dess 2,5972 gold badges22 silver badges43 bronze badges 5
  • 3 Replace the first . with something unique of your choice, then replace it back later? – Tuan Anh Hoang-Vu Commented May 14, 2015 at 0:10
  • hmm i would prefer a robust solution rather than like a hack for now. If there is not other way. I would definetley adapt your suggestion. Thank you @tuananh – Rahul Dess Commented May 14, 2015 at 0:12
  • 1 possible duplicate of Javascript replace, ignore the first match – Tuan Anh Hoang-Vu Commented May 14, 2015 at 0:14
  • @RahulDess I came back and added a self-contained version with no external variables. Please see my additional answer. – Drakes Commented May 15, 2015 at 0:17
  • Thanks @Drakes ..seen your update. – Rahul Dess Commented May 15, 2015 at 4:32
Add a ment  | 

1 Answer 1

Reset to default 9

You can pass in a function to replace, and skip the first match like this:

var i = 0;
value.replace(/[\.\%]/g, function(match) { 
    return match === "." ? (i++ === 0 ? '.' : '') : ''; 
});

Here is a self-contained version with no external variables:

value.replace(/[\.\%]/g, function(match, offset, all) { 
   return match === "." ? (all.indexOf(".") === offset ? '.' : '') : ''; 
}) 

This second version uses the offset passed into the replace() function to pare against the index of the first . found in the original string (all). If they are the same, the regex leaves it as a .. Subsequent matches will have a higher offset than the first . matched, and will be replaced with a ''. % will always be replaced with a ''.


Both versions result in:

4.5667.444... ==> 4.56667444
%4.5667.444... ==> 4.5667444

Demo of both versions: http://jsbin./xuzoyud/5/

发布评论

评论列表(0)

  1. 暂无评论