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 - Removing all instances of a character from a string - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Removing all instances of a character from a string - Stack Overflow

programmeradmin2浏览0评论

I'm attempting to remove all instances of a given set of characters £$€,. from a string in jQuery/Javascript. I'm using the replace function, however this only appears to remove a single instance of the character and not all of them.

For example consider the string:

1,500,00.00.$djdjd£10€10

I get back:

1500,0000.djdjd1010

As you can see, it only removes a single instance of each character. £, $ and are fine as there is only one of each in the string.

Here is what I have so far:

function validatePriceRange(value, min, max) {

    var replacements = ["£", "$", "€", ",", "."];

    $.each(replacements, function (index, item) {
        value = value.replace(item, "");
    });

    var value = parseInt(value, 10);

    return value >= min && value <= max;
}

jsFiddle

Can anyone spot what I've done wrong?

I'm attempting to remove all instances of a given set of characters £$€,. from a string in jQuery/Javascript. I'm using the replace function, however this only appears to remove a single instance of the character and not all of them.

For example consider the string:

1,500,00.00.$djdjd£10€10

I get back:

1500,0000.djdjd1010

As you can see, it only removes a single instance of each character. £, $ and are fine as there is only one of each in the string.

Here is what I have so far:

function validatePriceRange(value, min, max) {

    var replacements = ["£", "$", "€", ",", "."];

    $.each(replacements, function (index, item) {
        value = value.replace(item, "");
    });

    var value = parseInt(value, 10);

    return value >= min && value <= max;
}

jsFiddle

Can anyone spot what I've done wrong?

Share Improve this question edited Jul 24, 2014 at 11:46 DGibbs asked Jul 24, 2014 at 11:24 DGibbsDGibbs 14.6k7 gold badges49 silver badges88 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 11

replace called with a string as first argument does only one replacement, while using a regular expression with flag g replaces all occurrences.

Using a regular expression, you can also avoid looping over an array and do it in one pass :

value = value.replace(/£|\$|€|,|\./g,'');

You are only cycling through your replacement array once and replace everytime the specific character. But replace is only replacing the first occurance of a given string.

For a replace all method, look here.

I don't think you need a function for it:

var validated = parseInt('1,500,00.00.$djdjd£10€10'.replace(/[£$€,.]/g,''), 10);
//=> 15000000
// or if you want the validated directly
var validated = function(min,max) {
                  var v = parseInt('1,500,00.00.$djdjd£10€10'
                                    .replace(/[£$€,.]/g,''), 10);
                  return v >= min && v <==max;
                }(1000, 200000); //=> false

The regular expression should be different if you want to include all digits in the string:

var validated = function(min,max) {
                  var v = parseInt('1,500,00.00.$djdjd£10€10'
                                    .replace(/[^\d]/g,''), 10);
                  //                           ^ replace non numbers
                  // v now is 150000001010
                  return v >= min && v <==max;
                }(1000, 200000); //=> false

Use a regex with the global flag, which will search and replace all instances

var replacements = ["£", "\\$", "€", ",", "\\."];
$.each(replacements, function (index, item) {
    value = value.replace(new RegExp(item, "g"), '');
});

Demo: Fiddle

As already answered here you could use the following regex that replaces all non characters and whitespaces with empty.

var value = "1,500,00.00.$djdjd£10€10"
value = value.replace(/[^\w\s]/gi, '')
发布评论

评论列表(0)

  1. 暂无评论