我正在研究正则表达式并面临一个问题。我无法在node.js中使用正则表达式在字符串中找到一个,两个,三个,四个等。
示例:字符串包含第1章或第1章的时间。我能找到1而不是1。
Chapter one
Chapter two
Chapter three
Chapter four
.....
如何用文字查找数字?
任何人都可以帮助我吗?
答案 0 :(得分:1)
您可以尝试:
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+)?)/);
将返回["Chapter one", "one"]
。
模式描述:
/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