正则表达式/ | \[^ \ d + | \。+)/ g的意思吗?

时间:2021-04-06 01:58:41

The following code uses the regular expression /[^\d|\-+|\.+]/g. I want to know how the code works and what the expression does. Can anybody help me?

下面的代码使用正则表达式/ | \[^ \ d - + | \。+)/ g。我想知道代码是如何工作的,表达式是如何工作的。有人能帮助我吗?

app.$inject = ['$scope'];

app.directive('format', ['$filter', function ($filter) {
  return {
    require: '?ngModel',
    link: function (scope, elem, attrs, ctrl) {
        if (!ctrl) return;


        ctrl.$formatters.unshift(function (a) {
            return $filter(attrs.format)(ctrl.$modelValue)
        });


        ctrl.$parsers.unshift(function (viewValue) {
            var plainNumber = viewValue.replace(/[^\d|\-+|\.+]/g, '');
            elem.val($filter('number')(plainNumber));
            return plainNumber;
        });
    }
  };
}]);

1 个解决方案

#1


10  

It's always useful to have a tool to test these things. Regex are much harder to read than they are to write.

有一个工具来测试这些东西总是很有用的。Regex要比写的更难读。

Here's a visualization of how that regex in particular behaves:

下面是regex的具体行为的可视化:

正则表达式/ | \[^ \ d + | \。+)/ g的意思吗?

[] will match one of the characters found in the set, ^ means to match anything that's not in the set, and then \d|\-+|\.+ mean any digit (that's the \d), |, -, +, ., and a couple characters that were repeated.

[]将匹配一个字符集合,^表示匹配任何不在集合,然后| \ \ d + | \。+表示任何数字(那是\d)、|、-、+、以及重复的几个字符。

Also, for a basic understanding of regex you might want to check out this blog post.

此外,要了解regex的基本知识,您可能需要查看这篇博客文章。

#1


10  

It's always useful to have a tool to test these things. Regex are much harder to read than they are to write.

有一个工具来测试这些东西总是很有用的。Regex要比写的更难读。

Here's a visualization of how that regex in particular behaves:

下面是regex的具体行为的可视化:

正则表达式/ | \[^ \ d + | \。+)/ g的意思吗?

[] will match one of the characters found in the set, ^ means to match anything that's not in the set, and then \d|\-+|\.+ mean any digit (that's the \d), |, -, +, ., and a couple characters that were repeated.

[]将匹配一个字符集合,^表示匹配任何不在集合,然后| \ \ d + | \。+表示任何数字(那是\d)、|、-、+、以及重复的几个字符。

Also, for a basic understanding of regex you might want to check out this blog post.

此外,要了解regex的基本知识,您可能需要查看这篇博客文章。