Regex for one, two, three, etc. using Node.js

I am working on regex and am facing one problem. I cannot find one, two, three, four, etc. On a string using regex in node.js.

Example: The line contains some time in chapter 1 or first chapter. I can find 1, but not one.

 Chapter one
 Chapter two
 Chapter three
 Chapter four
 .....

      

How to find a number in words?

Can anyone help me?

+3


source to share


1 answer


You may try:

str = 'Chapter one';
str.match(/Chapter\s{1}(\w+)/);
// or
str.match(/Chapter (\w+)/);
// or, for: thirty three etc
str.match(/Chapter\s{1}(\w+(\s{1}\w+)?)/);

      



Will return ["Chapter one", "one"]

.
Template description:

/Chapter\s{1}(\w+)/
Chapter # will match only Chapter (case sensitive)
\s{1}   # one space (you can also use <space>)
(\w+)   # letter, at least one. You can refer to it as 1 element in returned array

      

+1


source







All Articles