.match()正则表达式的问题

时间:2015-05-10 22:56:41

标签: jquery regex

我有一些文本可以通过以下格式从方法返回:

"Title B-17G Flying Fortress Country United States of America Rank 4 Rating 6 Max altitude 11156 m Max speed 510.12 km/h on altitude 7680 m Turn time 36 s Take on distance 600 m Climb time 1680 s to altitude 7620 m Climb rate 4.5 m/s Time for free repair 108 h 16 m Max repair cost 6140 s.l. Cost 210000 s.l."

所以基本上,从上面我需要提取:

"B17G Flying Fortress", "United States of America", "4", "6", " 11156 m", "510.12 km/h on altitude 7620 m", ...

想法?

1 个答案:

答案 0 :(得分:2)

如果字段序列保持不变,您可以尝试这样的事情:

Title\s*(.*?)\s*Country\s*(.*?)\s*Rank\s*(.*?)\s*Rating\s*(.*?)\s*Max altitude\s*(.*?)\s*Max speed\s*(.*?)\s*Turn time\s*(.*?)\s*Take on distance\s*(.*?)\s*Climb time\s*(.*?)\s*Climb rate\s*(.*?)\s*Time for free repair\s*(.*?)\s*Max repair cost\s*(.*?)\s*Cost\s*(.*?)\s*$

https://ngrok.com/

var re = /Title\s*(.*?)\s*Country\s*(.*?)\s*Rank\s*(.*?)\s*Rating\s*(.*?)\s*Max altitude\s*(.*?)\s*Max speed\s*(.*?)\s*Turn time\s*(.*?)\s*Take on distance\s*(.*?)\s*Climb time\s*(.*?)\s*Climb rate\s*(.*?)\s*Time for free repair\s*(.*?)\s*Max repair cost\s*(.*?)\s*Cost\s*(.*?)\s*$/gm; 
var str = 'Title B-17G Flying Fortress Country United States of America Rank 4 Rating 6 Max altitude 11156 m Max speed 510.12 km/h on altitude 7680 m Turn time 36 s Take on distance 600 m Climb time 1680 s to altitude 7620 m Climb rate 4.5 m/s Time for free repair 108 h 16 m Max repair cost 6140 s.l. Cost 210000 s.l..';
var subst = '$1, $2, $3, $4, $5, $6, $7, $8, $9'; 

var result = str.replace(re, subst);

否则,您可以尝试使用相同的正则表达式