匹配两个超时之间的两位数字

时间:2015-12-04 05:52:30

标签: javascript regex

我正在使用以下正则表达式:

ExpenseId

匹配以下字符串的月份:

....
maintenance ObjMaintenance = new maintenance 
{ 
    MaintenanceId = Guid.NewGuid(),
    ....
};                   
foreach (var i in TVM.Expenses)
{
    i.ExpenseId = Guid.NewGuid(); // add this
    ObjMaintenance.Expenses.Add(i);
}
db.maintenances.Add(ObjMaintenance);
db.SaveChanges();

它的作用......差不多:

(?:-)[0-9]{2}(?=-)

包括左派宣传。如何仅匹配2015-11-24T06:46:43.383Z (两位数字)?

2 个答案:

答案 0 :(得分:2)

字边界起作用:

"2015-11-24T06:46:43.383Z".match(/\b\d{2}\b/)[0]
#=> "11"

如果必须是连字符:

"2015-11-24T06:46:43.383Z".match(/-(\d{2})-/)[1]
#=> "11"

答案 1 :(得分:0)

不要使用RegEx ,请在JavaScript中使用var month = new Date('2015-11-24T06:46:43.383Z').getMonth();

getMonth

请注意,月份是从零开始的索引,因此'2015-11-24T06:46:43.383Z'.match(/-([0-9]{2})-/)[1]; 将返回少于过去月份的月份。

如果仍然想要使用正则表达式,请使用捕获组

class CategoryForm(forms.ModelForm):
    class Meta:
        model = models.Category
        exclude = ()
        form = CategoryForm

class CategoryAdmin(admin.ModelAdmin):
    list_display = ['department','name']
    list_filter = ['department','name']

    def get_form(self, request, obj=None, **kwargs):
        form = super(CategoryAdmin, self).get_form(request, obj=None, **kwargs)
        form.base_fields['department'].queryset = Department.objects.filter(
            name = request.user.customuser.department.name)
        return form