简体   繁体   English

Javascript正则表达式-仅匹配一组4个数字

[英]Javascript Regex - match on only one set of 4 numbers

I'm new to regex and and having trouble coming up an expression (javascript) to match what I need. 我是regex的新手,遇到了表达式(javascript)匹配我所需要的麻烦。

For this example: 对于此示例:

21 Apr 1789 -  4 Mar 1797  John Adams                        (b. 1735 - d. 1826)  Fed 
 4 Mar 1797 -  4 Mar 1801  Thomas Jefferson                  (b. 1743 - d. 1826)  D-R 
 4 Mar 1801 -  4 Mar 1805  Aaron Burr                        (b. 1756 - d. 1836)  D-R 
 4 Mar 1805 - 20 Apr 1812  George Clinton                    (b. 1739 - d. 1812)  D-R 
 4 Mar 1813 - 23 Nov 1814  Elbridge Gerry                    (b. 1744 - d. 1814)  D-R 
 4 Mar 1817 -  4 Mar 1825  Daniel D. Tompkins                (b. 1744 - d. 1825)  D-R 
 4 Mar 1825 - 28 Dec 1832  John Caldwell Calhoun             (b. 1782 - d. 1850)  Dem 
 4 Mar 1833 -  4 Mar 1837  Martin van Buren                  (b. 1782 - d. 1862)  Dem 
 4 Mar 1837 -  4 Mar 1841  Richard Mentor Johnson            (b. 1780 - d. 1850)  Dem 
 4 Mar 1841 -  4 Apr 1841  John Tyler                        (b. 1790 - d. 1862)  Whg  
 4 Mar 1845 -  4 Mar 1849  George Mifflin Dallas             (b. 1792 - d. 1864)  Dem 

What I need from each row is only the 4 digit year that comes after the "b. ". 我需要的每一行只是“ b。”之后的4位数字年份。 So if I returned only my matches, it would be: 因此,如果我只返回我的比赛,它将是:

1735
1743
1756
1739
1744
1744
1782
1782
1780
1790
1792

No matter how I try to build the expression, whether on my own or using any of the online web apps, it always wants to match on all the 4-digit years, or, all 4-digit years up to and including the one I need. 无论我是如何尝试构建表达式,无论是自己创建还是使用任何在线Web应用程序,它始终希望在所有 4位数字年份或直到我(包括我)的所有4位数字年份匹配需要。 This is certainly due in part to lack of experience on my side, so any help would be greatly appreciated. 当然,部分原因是我缺乏经验,因此我们将不胜感激。

Using the regular expression /b\\. (\\d{4})/ 使用正则表达式/b\\. (\\d{4})/ /b\\. (\\d{4})/ should match all of the birth dates in the list. /b\\. (\\d{4})/应该与列表中的所有出生日期匹配。

You can use this regex: b\\.\\s(\\d{4}) and access capture group 1 to get the values you want. 您可以使用以下正则表达式: b\\.\\s(\\d{4})并访问捕获组1以获取所需的值。

Try it here: https://regex101.com/r/zxbrs0/1 在这里尝试: https : //regex101.com/r/zxbrs0/1

The easiest way to do this (in Javascript, since you tagged Javascript on this question) is simply to do two regular expressions. 执行此操作的最简单方法(在Javascript中,因为您在此问题上标记了Javascript)只是执行两个正则表达式。 For example, you could do this. 例如,您可以这样做。

var str = '4 Mar 1797  John Adams                        (b. 1735 - d. 1826)';
var regex = /\(b. [0-9]+/;
var regex2 = /[0-9]+/;
var birthDate = regex2.exec(regex.exec(str));
return birthDate;

If you get the general idea it's not hard to adapt it to your circumstances. 如果您有了一般的想法,那么就可以根据自己的情况进行调整。 Just use a regex to find (b. 1735 (in this case) and then extract 1735. This is only necessary because, unfortunately, regex in Javascript doesn't allow for the same look behind feature that other regex engines do. In another platform you could use (?<=\\(b. ) to just look behind the b. 只需使用正则表达式来查找(b。1735(在这种情况下),然后提取1735。这仅是必要的,因为不幸的是,Javascript中的正则表达式不允许其他正则表达式引擎具有相同的外观特性。您可以使用(?<= \\(b。)只看b的后面。

I've tested this script in Javascript, so it should work. 我已经用Javascript测试了此脚本,因此它应该可以工作。

Alternatively you could also use capturing parentheses. 或者,您也可以使用捕获括号。 Try this in place of the regular expressions and var birthDate above: 尝试使用它代替上面的正则表达式和var birthDate:

var regex = /\(b. ([0-9]+)/;
var birthDate = regex.exec(str)[1];

Just remember that exec() should return an array so you'll need to specify that you want the second element ([1]). 只需记住exec()应该返回一个数组,所以您需要指定您想要第二个元素([1])。

This is also tested. 这也已经过测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM