最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - What is the meaning of this regexp? - Stack Overflow

programmeradmin1浏览0评论

i need to figure out what this regular expression means?

"^[A-Z]{3}-[4-7]\d{2,4}\$$"

I think it starts with exactly 3 letters and ends with 2,3 or 4 digits (also not sure about the dubble $-sings) .

But I can't understand what this means:

     -[4-7]  

And I'm also not sure why there are 2 $ at the end ...

thanks

i need to figure out what this regular expression means?

"^[A-Z]{3}-[4-7]\d{2,4}\$$"

I think it starts with exactly 3 letters and ends with 2,3 or 4 digits (also not sure about the dubble $-sings) .

But I can't understand what this means:

     -[4-7]  

And I'm also not sure why there are 2 $ at the end ...

thanks

Share Improve this question asked Aug 19, 2013 at 15:31 nclsvhnclsvh 2,8686 gold badges34 silver badges53 bronze badges 1
  • This is basic regex syntax. Check out: regular-expressions.info. – ridgerunner Commented Aug 19, 2013 at 15:39
Add a ment  | 

5 Answers 5

Reset to default 11
  • ^ the start of the string
  • [A-Z]{3} a character from A to Z repeated 3 times
  • - the character -
  • [4-7] a digit from 4 to 7
  • \d{2,4} any digit from 0 to 9 repeated between 2 and 4 times
  • \$ the character $
  • $ the end of the string

Regular Expressions 101

Go to regex101. and paste the regex in there...it will describe it to you. This will allow you to also test your regex within your browser.

give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime

REGEX

/"^[A-Z]{3}-[4-7]\d{2,4}\$$"/

Description

" Literal "
^ Start of string
Char class [A-Z] 3 times [greedy] matches:
    A-Z A character range between Literal A and Literal Z
    - Literal -
Char class [4-7]  matches:
    4-7 A character range between Literal 4 and Literal 7
\d 2 to 4 times [greedy] Digit [0-9]
\$ Literal $
$ End of string
" Literal "

Visualization (thanks to naomik) provided by debuggex

Visualize!

Also, that's not a Regular Expression (RegExp); that's just a string.

If you want to make it a RegExp:

var re = new RegExp("^[A-Z]{3}-[4-7]\\d{2,4}\\$$");

Or just

var re = /^[A-Z]{3}-[4-7]\d{2,4}\$$/;

-[4-7] means the character - followed by one of the characters 4, 5, 6 and 7.

The first $ is escaped - so it indicates a $ in the input, whereas the second $ is not escaped, and so it indicates the end of the string.

-: this is literally the minus - character.

[4-7]: a single digit, either 4,5,6 or 7.

\$ is just an escaped $ sign, so it's interpreted as text, and not as "end of string".

发布评论

评论列表(0)

  1. 暂无评论