Which regular expression will detect if a string is part of a numbered list?

I am really trying to find a regex that will reliably check if a string is part of a numbered list (for example, those you find in a common word processor).

So I would like it to return true if the beginning of the line is a number followed by fullstop and a space .

For single or even double-digit numbers, I can of course do this easily without the need for regular expressions, but since the number can be of any size, it will not work.

Example 1

"1. This is a string" 

Should return true , because the line starts with: "1."

Example 2

 "3245. This is another string" 

Should return true , because the line starts with: "3245."

Example 3

 "This is a 24. string" 

Must return false

Since there is no pattern matching at the beginning of the line ... however ...

Example 4

 "3. This is 24. string" 

Should still return true

Hope this is clear enough.

Thanks in advance for your help.

+4
source share
2 answers

This is a fairly simple regular expression. The following will find the beginning of the line, then the digit character will repeat one or more times, then the period character, then the whitespace character.

 ^\d+\. 

Rey

In JavaScript, you can follow these steps to validate a string:

 var startsWithNumber = /^\d+\. /m.test('34. this should return true.'); 

jsFiddle

+8
source

You could just do it.

/^\d+\.\s+/ or even /^\d*\.\s*/

Explanation of the + and * operators

 \d Any digit \s Any whitespace character * Zero or more + One or more 
+1
source

Source: https://habr.com/ru/post/1496893/


All Articles