如何匹配Javascript正则表达式中的整个语句?

时间:2022-08-17 15:44:44

I need to create a Regular Expression (in Javascript) that will match a specific sentence structure. In my case, it's "Course ### day ###", where ### can be any 1 to 3 digit number. Additionally, it can be "week" instead of "day", so "Course ### week ###" is also valid.

我需要创建一个正则表达式(用Javascript)来匹配特定的句子结构。在我的例子中,是“### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ## @ ### ### ### #此外,它可以是“周”而不是“天”,因此“课程### #周##”也是有效的。

So far, I've come up with:

到目前为止,我想到了:

var regTest=/^(Course) \d{1,3} (day)|(week) \d{1,3}$/

The problem is that this expression matches "Course 9 day 1", which is what I want, but it would also match "Course 9 day 1 scheduled on 07/01/09".

问题是这个表达式匹配“课程9天1”,这是我想要的,但是它也会匹配“课程9天1计划在07/01/09”。

In other words, the following returns a value of 0:

换句话说,以下返回的值为0:

"Course 9 day 1".search(regTest)

But this one also returns 0:

但这个也返回0:

"Course 9 day 1 scheduled on 07/01/09".search(regTest)

I want the first test to return 0, but I want the second one to return -1. I need the expression to ONLY match up until the ### following day/week. How do you make it to stop at that point?

我希望第一个测试返回0,但我希望第二个测试返回-1。我需要这个表达式只匹配到###下一天/下一周。你怎么让它在那点停下来?

4 个解决方案

#1


3  

You need to move the OR (the pipe) into the parenthesis between the day and week:

你需要把OR(管道)移到日和周之间的括号中:

/^(Course) \d{1,3} (day|week) \d{1,3}$/

If the pipe is inside the parens then it matches "day" or "week" in that position. With the pipe not inside of a parens it matches either the first half of the expression (i.e. "Course ### day") or the last half of the expression (i.e. "week ###").

如果管道位于parens的内部,那么它将在该位置匹配“day”或“week”。如果管道不在parens内部,那么它将匹配表达式的前半部分(即“课程### ## day”)或表达式的后半部分(例如。“周# # #”)。

Also, if you are just trying to validate that the string matches the pattern you can use "test" rather than "search":

另外,如果您只是想验证字符串是否与模式匹配,可以使用“test”而不是“search”:

if(/^(Course) \d{1,3} (day|week) \d{1,3}$/.test(someString)) {
    // it matches!
}

#2


0  

An additional level of grouping is needed:

需要增加一个分组级别:

var regTest=/^(Course) \d{1,3} ((day)|(week)) \d{1,3}$/

#3


0  

You want

你想要的

var regTest=/^(Course) \d{1,3} (day|week) \d{1,3}$/

(Notice that (day)|(week) has changed to (day|week)). Your old regex was actually two, joined with the |. You see, | is one of the less (least?) tightly binding regex metacharacters, which means that ab|cd will match "ab" and "cd", but not "abd" or "acd".

(注意(天)|(周)已改为(天|周))。你的旧regex实际上是两个,与|一起。你看,|是一个更少的(最少?)紧密结合的regex元字符,这意味着ab|cd将匹配“ab”和“cd”,而不是“abd”或“acd”。

#4


0  

You have everything right except that you need to put the check for (day)|(week) in a parenthesis block, i.e.

除了需要将(day)|(week)的检查放在括号中之外,其他都是正确的。

var regTest=/^(Course) \d{1,3} ((day)|(week)) \d{1,3}$/

You can actually reduce the statement to

你可以把这个声明减少到。

var regTest=/^Course \d{1,3} (day|week) \d{1,3}$/

BTW, I do know why you don't need the parenthesis around Course. I'm not sure why you don't need them around day and week. If I had access to sed right know I would like to see how it treated ((day)|(week)) versus (day|week).

顺便说一句,我知道你为什么不需要在课程前后加上括号。我不知道你为什么不需要它们。如果我有正确的sed,我想看看它是如何处理的(日)|(周)和(日|周)。

#1


3  

You need to move the OR (the pipe) into the parenthesis between the day and week:

你需要把OR(管道)移到日和周之间的括号中:

/^(Course) \d{1,3} (day|week) \d{1,3}$/

If the pipe is inside the parens then it matches "day" or "week" in that position. With the pipe not inside of a parens it matches either the first half of the expression (i.e. "Course ### day") or the last half of the expression (i.e. "week ###").

如果管道位于parens的内部,那么它将在该位置匹配“day”或“week”。如果管道不在parens内部,那么它将匹配表达式的前半部分(即“课程### ## day”)或表达式的后半部分(例如。“周# # #”)。

Also, if you are just trying to validate that the string matches the pattern you can use "test" rather than "search":

另外,如果您只是想验证字符串是否与模式匹配,可以使用“test”而不是“search”:

if(/^(Course) \d{1,3} (day|week) \d{1,3}$/.test(someString)) {
    // it matches!
}

#2


0  

An additional level of grouping is needed:

需要增加一个分组级别:

var regTest=/^(Course) \d{1,3} ((day)|(week)) \d{1,3}$/

#3


0  

You want

你想要的

var regTest=/^(Course) \d{1,3} (day|week) \d{1,3}$/

(Notice that (day)|(week) has changed to (day|week)). Your old regex was actually two, joined with the |. You see, | is one of the less (least?) tightly binding regex metacharacters, which means that ab|cd will match "ab" and "cd", but not "abd" or "acd".

(注意(天)|(周)已改为(天|周))。你的旧regex实际上是两个,与|一起。你看,|是一个更少的(最少?)紧密结合的regex元字符,这意味着ab|cd将匹配“ab”和“cd”,而不是“abd”或“acd”。

#4


0  

You have everything right except that you need to put the check for (day)|(week) in a parenthesis block, i.e.

除了需要将(day)|(week)的检查放在括号中之外,其他都是正确的。

var regTest=/^(Course) \d{1,3} ((day)|(week)) \d{1,3}$/

You can actually reduce the statement to

你可以把这个声明减少到。

var regTest=/^Course \d{1,3} (day|week) \d{1,3}$/

BTW, I do know why you don't need the parenthesis around Course. I'm not sure why you don't need them around day and week. If I had access to sed right know I would like to see how it treated ((day)|(week)) versus (day|week).

顺便说一句,我知道你为什么不需要在课程前后加上括号。我不知道你为什么不需要它们。如果我有正确的sed,我想看看它是如何处理的(日)|(周)和(日|周)。

相关文章