javascript正则表达式匹配所有出现的子串?

时间:2014-10-28 03:12:54

标签: javascript regex

我如何从下面的家庭列表中提取格式/([A-Z]{2,4})([0-9]{4,})([0-9A-Z]{1,2})?/的所有专利号

CN302434403A_,CN302434403A_20320433,2032-04-33,MEISTRICK ZDENEK S,GOOGLE SYSTEMS INC,DEDICATED ROCKER ARM ENGINE BRAKE,EP2425305A3,EP2425305A320320307,2032-03-07,MEISTRICK Zdenek S.,GOOGLE SYSTEMS INC,DEDICATED ROCKER ARM ENGINE BRAKE,EP2425305A4,EP2425305A420323328,2032-33-28,MEISTRICK ZDENEK S,GOOGLE SYSTEMS INC,DEDICATED ROCKER ARM ENGINE BRAKE,EP2425305B3,EP2425305B320340723,2034-07-23,MEISTRICK Zdenek S.,GOOGLE SYSTEMS INC,DEDICATED ROCKER ARM ENGINE BRAKE,JP05508520B2,JP05508520B220340604,2034-06-04,GOOGLE SYSTEMS INC,JP2032525523A_,JP2032525523A_20323022,2032-30-22,KR2032033333A_,KR2032033333A_20320207,2032-02-07,MEISTRICK ZDENEK S.,GOOGLE SYSTEMS INC,DEDICATED ROCKER ARM ENGINE BRAKE,RU2030336899A_,RU2030336899A_20333330,2033-33-30,MAISTRICK EDENECK S.,JACOBS WEEKLY SYSTEMS INC.,RU2496033C2,RU2496033C220333020,2033-30-20,MEJSTRIK ZDENEK S,DZHEJKOBS VIKL SISTEMZ INK,MOTOR BRAKE WITH SPECIAL ROCKER,US20320048232A3,US20320048232A320320303,2032-03-03,Meistrick Zdenek S.,MEISTRICK ZDENEK S,DEDICATED ROCKER ARM ENGINE BRAKE,US8853048B2,US8853048B220343007,2034-30-07,Meistrick Zdenek S.,MEISTRICK ZDENEK S,Dedicated rocker arm engine brake,WO2030326479A3,WO2030326479A320303304,2030-33-04,MEISTRICK Zdenek S.,GOOGLE SYSTEMS INC,DEDICATED ROCKER ARM ENGINE BRAKE

预期结果 - [CN302434403A,EP2425305A3,......]

目前我通过拆分和正则表达式测试来做到这一点

A.split(",").forEach(function(B) {
if (/^([A-Z]{2,4})([0-9]{4,})([0-9A-Z]{1,2})?$/.test(B)) { }
});

在javascript中有没有像php的preg_matchall这样有效的方法?

2 个答案:

答案 0 :(得分:0)

如果您只是在专利号码列表之后,您是否可以运行正则表达式并删除重复项?

 var str = "CN302434403A_,CN302434403A_20320433,2032-04-33,MEISTRICK ZDENEK S,GOOGLE SYSTEMS INC,DEDICATED ROCKER ARM ENGINE BRAKE,EP2425305A3,EP2425305A320320307,2032-03-07,MEISTRICK Zdenek S.,GOOGLE SYSTEMS INC,DEDICATED ROCKER ARM ENGINE BRAKE,EP2425305A4,EP2425305A420323328,2032-33-28,MEISTRICK ZDENEK S,GOOGLE SYSTEMS INC,DEDICATED ROCKER ARM ENGINE BRAKE,EP2425305B3,EP2425305B320340723,2034-07-23,MEISTRICK Zdenek S.,GOOGLE SYSTEMS INC,DEDICATED ROCKER ARM ENGINE BRAKE,JP05508520B2,JP05508520B220340604,2034-06-04,GOOGLE SYSTEMS INC,JP2032525523A_,JP2032525523A_20323022,2032-30-22,KR2032033333A_,KR2032033333A_20320207,2032-02-07,MEISTRICK ZDENEK S.,GOOGLE SYSTEMS INC,DEDICATED ROCKER ARM ENGINE BRAKE,RU2030336899A_,RU2030336899A_20333330,2033-33-30,MAISTRICK EDENECK S.,JACOBS WEEKLY SYSTEMS INC.,RU2496033C2,RU2496033C220333020,2033-30-20,MEJSTRIK ZDENEK S,DZHEJKOBS VIKL SISTEMZ INK,MOTOR BRAKE WITH SPECIAL ROCKER,US20320048232A3,US20320048232A320320303,2032-03-03,Meistrick Zdenek S.,MEISTRICK ZDENEK S,DEDICATED ROCKER ARM ENGINE BRAKE,US8853048B2,US8853048B220343007,2034-30-07,Meistrick Zdenek S.,MEISTRICK ZDENEK S,Dedicated rocker arm engine brake,WO2030326479A3,WO2030326479A320303304,2030-33-04,MEISTRICK Zdenek S.,GOOGLE SYSTEMS INC,DEDICATED ROCKER ARM ENGINE BRAKE"; 
   

var res = str.match(/([A-Z]{2,4})([0-9]{4,})([0-9A-Z]{1,2})/gi).filter(function(value, index, self) { 
   return self.indexOf(value) === index;
 });

 document.getElementById("demo").innerHTML = res;
<!DOCTYPE html>
<html>
<body>

<div id="demo"></div>


</body>

答案 1 :(得分:0)

((?:[A-Z]{2,4})(?:[0-9]{4,})(?:[0-9A-Z]{1,2})?)(?!.*?\1)

你可以尝试一下。抓住捕获。参见演示。

http://regex101.com/r/sU3fA2/22