7

What does \d do in this example?

/Chapter (\d+)\.\d*/
1
  • 2
    Different things depending on where you put it. Commented Dec 27, 2010 at 19:14

3 Answers 3

11

In what context?

In a regular expression, it matches a digit (0-9).

Edit, according to your comment:

It matches any string starting with Chapter, followed by digits, then a dot, then a number of digits. Like Chapter 1.0 and Chapter 12.01.

Sign up to request clarification or add additional context in comments.

5 Comments

/Chapter (\d+)\.\d*/ <- how is it used?
It matches any string starting with Chapter, followed by digits, then a dot, then a number of digits. Like Chapter 1.0 and Chapter 12.01. You should update your original question with that one.
That is a regular expression. It helps matching patterns. Google has plenty of information about it.
It's for grouping. You can access a group by \1 for the first group, \2 for the second and so on. More info here daaq.net/old/javascript/…
It would also match "Chapter 1.", without anything after the dot. Anyway, it looks like the purpose is actually to detect the main chapter number, right before the dot, which is what the parens do.
4

In a regex it means digits 0-9

\d Digits 0 through 9 /H\d/ matches "H3"

Comments

2

If it is in a regular expression (match or replace or split) or a /.../ string then it probably means match any digit 0-9. Please provide the code you see it in so we can be sure.

2 Comments

/Chapter (\d+)\.\d*/ <- how is it used?
That is a regular expression to match the word Chapter followed by space, then one or more digits (\d+) (these first digits are captured to $1 I don't know if you are using $1 or ignoring it). After that it matches a dot \., then it is allowed but not required to have digits after the dot \d*

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.