简体   繁体   English

无法在Javascript中定义适当的RegEx

[英]Trouble defining a proper RegEx in Javascript

I need to create a regular expression that matches a process number that has the following pattern #######-##.####.7.09.0009 where # means numbers from 0 to 9. Here is what I came up with after some research: 我需要创建一个正则表达式,以匹配具有以下模式的进程号#######-##。####。7.09.0009,其中#表示从0到9的数字。这就是我来的内容经过一些研究:

var regex = new RegExp("^[0-9]{7}[\-][0-9]{2}[\.][0-9]{4}[\.7\.09\.0009]$");

I also tried: 我也尝试过:

  • /^[0-9]{7}\\-[0-9]{2}\\.[0-9]{4}\\.7\\.09\\.0009$/ /^[0-9]{7}\\-[0-9]{2}\\.[0-9]{4}\\.7\\.09\\.0009$/
  • /^[0-9]{7}\\\\-[0-9]{2}\\\\.[0-9]{4}\\\\.7\\\\.09\\\\.0009$/ /^[0-9]{7}\\\\-[0-9]{2}\\\\.[0-9]{4}\\\\.7\\\\.09\\\\.0009$/

Try this: 尝试这个:

const pattern = /\d{7}\-\d{2}\.\d{4}\.7\.09\.0009/

Regexper is a great tool that I use whenever I'm writing a regular expression, I find it really helps to visualize what the expression is actually doing. Regexper是一个很棒的工具,每当我编写正则表达式时,我都会发现它确实有助于可视化表达式的实际作用。 Check it out. 看看这个。

For reference, here is the original pattern that you posted -- it looks like the main problem is that you are defining character classes in several places using [ and ] where you really don't need them at all. 作为参考, 这是您发布的原始模式 -似乎主要的问题是您正在使用[]在几个地方定义字符类,而实际上根本不需要它们。

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

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