首页 \ 问答 \ glMatrix不工作?(glMatrix not working?)

glMatrix不工作?(glMatrix not working?)

当我记录我的this.kernel时,值不会改变。 但我认为他们应该这样做。 遵循文档它应该工作, http://glmatrix.net/docs/2.2.0/symbols/vec3.html#.normalize

    this.kernel = [];
    this.kernelSize = 16.0;
    var max = 1.0;
    var min = -1.0;
    var a = Math.random(); 

for (var i = 0; i < this.kernelSize; i++){
    this.kernel.push(Math.random() * (max - min) + min); // random float, range -1..1
    this.kernel.push(Math.random() * (max - min) + min);
    this.kernel.push(Math.random()); //random float, range 0..1

    vec3.normalize([this.kernel[i * 3], this.kernel[i * 3 + 1], this.kernel[i * 3 + 2]], [this.kernel[i * 3], this.kernel[i * 3 + 1], this.kernel[i * 3 +2]]);
    console.log(this.kernel);
    vec3.multiply([this.kernel[i * 3], this.kernel[i * 3 + 1], this.kernel[i * 3 + 2]], a, 1.0);
    console.log(this.kernel);

}

When I log my this.kernel, the values don't change. But I think they should. Following the documentation it should work, http://glmatrix.net/docs/2.2.0/symbols/vec3.html#.normalize

    this.kernel = [];
    this.kernelSize = 16.0;
    var max = 1.0;
    var min = -1.0;
    var a = Math.random(); 

for (var i = 0; i < this.kernelSize; i++){
    this.kernel.push(Math.random() * (max - min) + min); // random float, range -1..1
    this.kernel.push(Math.random() * (max - min) + min);
    this.kernel.push(Math.random()); //random float, range 0..1

    vec3.normalize([this.kernel[i * 3], this.kernel[i * 3 + 1], this.kernel[i * 3 + 2]], [this.kernel[i * 3], this.kernel[i * 3 + 1], this.kernel[i * 3 +2]]);
    console.log(this.kernel);
    vec3.multiply([this.kernel[i * 3], this.kernel[i * 3 + 1], this.kernel[i * 3 + 2]], a, 1.0);
    console.log(this.kernel);

}

原文:https://stackoverflow.com/questions/17382604
更新时间:2023-10-17 17:10

最满意答案

首先,分析您当前的正则表达式以及它为什么不起作用:

  • @是文字@字符,这里没什么可看的
  • [...]是一个角色类。 它将匹配它包含的任何(一个)字符
  • [az\d_]是一个由每个小写字母,每个数字(由它们自己的字符类\d )和下划线组成的字符类
  • +是一个量词,这意味着它修改的令牌必须至少匹配一次并且可以匹配多次。 这里它适用于前一个字符类
  • /pattern/flags是Javascript的正则表达式语法之一
  • i是不区分大小写的标志。 在这种情况下,它意味着字符类也将匹配大写字母,尽管它只包含小写字母
  • g是全球旗帜。 这意味着正则表达式将尝试匹配多个结果,而不是在第一次遇到时返回。

所以你试图匹配@User Name Can Have Spaces(userId: number) ,但你的正则表达式与你提到的空格不匹配,也不是括号。

您可以将这三个字符添加到字符类中,如下所示:

/@([a-z\d_ ()]+)/gi

但是,至少在我看来,对你想要匹配的东西的更好的描述将是:

/@[a-z\d_ ]+\(\d+\)/gi

我们匹配的用户名可以包含字母,数字,下划线和空格,后跟左括号,数字和右括号。 括号必须被转义,因此它们被理解为文字字符而不是正则表达式组。

如果要分别轻松提取用户名和用户ID,您可能希望使用以下内容,它们分别在各自的组中分组:

/@([a-z\d_ ]+)\((\d+)\)/gi

这是一个测试它的regex101链接


First, an analysis of your current regex and why it doesn't work :

  • @ is the literal @ character, nothing to see here
  • [...] is a character class. It will match any (one) of the characters it contains
  • [a-z\d_] is a character class composed of every lowercase letter, every digit (represented by their own character class \d) and the underscore
  • + is a quantifier which means the token it modifies must be matched at least once and can be matched more than once. Here it applies to the previous character class
  • /pattern/flags is one of Javascript's regular expressions syntax
  • i is the case-insensitive flag. In this case, it means the character class will also match uppercase letters although it only contains lowercase letters
  • g is the global flag. It means that the regex will attempt to match multiple results rather than returning on the first encountered.

So you're trying to match @User Name Can Have Spaces(userId: number), but your regex does not match spaces as you mentionned, nor parenthesis.

You could add these three characters to the character class, as follows :

/@([a-z\d_ ()]+)/gi

However, a better traduction of what you're trying to match, in my opinion at least, would be the following :

/@[a-z\d_ ]+\(\d+\)/gi

Where we match an username that can contain letters, digits, underscores and spaces, followed by an opening parenthesis, a number and a closing parenthesis. The parenthesis must be escaped so they are understood as the literal character rather than an regex group.

If you want to extract easily the username and the user id separately, you might want to use the following, where they are each grouped in their respective group :

/@([a-z\d_ ]+)\((\d+)\)/gi

Here's a regex101 link to test it.

相关问答

更多
  • 请注意,第二个匹配(来自