Matching Regular Expressions Count in jQuery

I need to count a group of regular expressions in a dynamically loaded <div>one that I loaded using the load () function. I also need to resize <div>to the longest string of characters in it. Is there any way to achieve this? I tried searching and can't find anything, even on SO. I should note that the expression I'm testing is:

Sat Mar 12 12:45:38 PST 2011

With this regex:

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}/))
+3
source share
3 answers
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

The regex JavaScript match function returns an array, so you can basically make a length in this array and get the size of the matching elements. Make sure you use gRegEx to search for all events.

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
+5

kjy112 . , jQuery, Javascript RegEx (, ).

- , - - nique, , :

$('#result').text().match(/\d{4}/).length
+1

Malfy,

3 . , , . , .

1) (fastest)

, div, white-space: nowrap, .

$('div.someClass').css('whiteSpace','nowrap');




2) (slowest)

div, - , , css . :

var yourString = 'your string';
// create a div containing your string
var $tempDiv = jQuery('<div style="visibility:hidden;position:absolute;white-space:nowrap">'+jQuery.trim(yourString)+'</div>').appendTo('body');
$newDiv = <your new div, however you're creating it>;
// set the width of the new div to the width of the temp div
$newDiv.width($tempDiv.width());
// and clean up;
$tempDiv.remove();
//repeat as necessary




3) (quite fast too)

Alternatively, if you are sure that you will use a monospace font (courier, console, etc.). There is a much faster way. Keep the width of one character and multiply it by the length of each new text line. This way you are not writing a new item every time. For instance:

var $tempDiv = $('<div style="visibility:hidden;margin:0;padding:0;border:0;">z</div>').appendTo('body');
//(any character will work. z is just for example); 
var reusableCharacterWidth=$tempDiv.width();
$tempDiv.remove();

var firstString = your string';
// set the width of your first div
$newDiv.width(reusableCharacterWidth*firstString.length);
var nextString = 'your next string';
// set the width of your next div
$nextNewDiv.width(reusableCharacterWidth*nextString.length);

(note: you can use $ .trim () for strings just in case)

To get the longest string:

var longestLineLength,
    yourText= 'your text here';
function getLongestLineLength(lines){
    var oneLineLength,
        longest=0,
        linesArray = lines.split('\n');
    for(var i=0,len=linesArray.length;i<len;i++){
        oneLineLength=linesArray[i].length;
        longest=oneLineLength>longest?oneLineLength:longest;
    }
  return longest;
}
longestLineLength = getLongestLineLength(yourText);

Hooray!

Adam

0
source

All Articles