在jQuery中计算正则表达式匹配

atx*_*atx 3 html javascript css jquery

我需要计算一组动态加载的正则表达式<div>,我使用load()函数加载它.我还需要将其调整<div>为其中最长的字符行.有没有办法实现这个目标?我试过四处寻找,找不到任何东西,甚至连SO都找不到.我应该提一下,我正在测试的表达式是:

Sat Mar 12 12:45:38 PST 2011
Run Code Online (Sandbox Code Playgroud)

使用这个正则表达式:

if ($('#result').text().match(/[A-Za-z]{3}\s[A-Za-z]{3}\s[0-9]{1,2}\s[0-9]{2}:[0-9]{2}:[0-9]{2}\s[A-Z]{3}\s[0-9]{4}/))
Run Code Online (Sandbox Code Playgroud)

KJY*_*葉家仁 5

var str="The rain in SPAIN stays mainly in the plain"; 
var patt1=/ain/gi;  //noticed the g.  g will enable match of all occurance, and without it it'll only match the first occurance
console.log(str.match(patt1).length);  //4 matched
Run Code Online (Sandbox Code Playgroud)

JavaScript匹配正则表达式函数返回一个数组,因此您基本上可以在该数组上执行一个长度并获得匹配元素的大小.确保您使用gRegEx中的所有内容进行搜索

根据您的RegEx,您可以执行以下操作:

$('#result').text().match(/[A-Za-z]{3}\s[A-Za-z]{3}\s[0-9]{1,2}\s[0-9]{2}:[0-9]{2}:[0-9]{2}\s[A-Z]{3}\s[0-9]{4}/g).length //this should give you the total count of occurance
Run Code Online (Sandbox Code Playgroud)