简体   繁体   English

这个jQuery / Regex代码段到底在做什么?

[英]What exactly is this jQuery / Regex snippet doing?

I'm not very good with RegEx, and am trying to learn. 我对RegEx不太满意,正在尝试学习。 I have inherited a project which contains the following line of code: 我继承了一个包含以下代码行的项目:

function findCourseIdFromForm(where) {
    var matchRegex = /\[course-[0-9]*\]/;
    var replaceRegex = /\[|\]|[a-z]|\-/g;
    return $(".cnumber", where.parent()).attr("name").match( matchRegex )[0].replace( replaceRegex,"" );
}

I'm not entirely sure what this piece of code is trying to do, but I know that it is causing issues on my page. 我不完全确定这段代码要做什么,但是我知道它会导致页面上的问题。 I'm using jQuery validator and this specific component (".cnumber") is causing the validation to fail and I'm not entirely sure why, so some insight into this line is appreciated. 我使用的是jQuery验证程序,并且此特定组件(“ .cnumber”)导致验证失败,我不确定原因为何,因此希望对此行有所了解。

The .cnumber field in the HTML looks like this: HTML中的.cnumber字段如下所示:

<input type="hidden" name="courses[course-0][cnumber]" class="cnumber" />

它从标记中的名称属性中删除[] ,小写字母和- ,大概是返回课程号。

It just get the course number. 它只是获得课程号。 In you example, it return 0 在您的示例中,它返回0

This function takes where node, gets its parent, finds .cnumber node within the parent, then takes [course-0] part and finally remove all [ , ] , - , and lowercase letters. 此函数采用where节点,获取它的父,认定.cnumber父节点内,然后采取[course-0]部分和最后去除所有[]-和小写字母。

So the function returns the number that stands after [course- part, or empty string if there is no number 因此该函数返回位于[course- part之后的数字;如果没有数字,则返回空字符串

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM