JavaScript Matrix Search

http://stackoverflow.com/questions/41272168/compare-text-inside-2-div-elements-using-javascript-or-jquery/

by Brian Kueck

HTML

Matrix 1: Expectation: Should be in matrix 2.
<pre class="one">
    g
   tuv
    J
</pre>

Matrix 2:
<pre class="two">
    a b   cdef  g hi  j k l
    m no pqr s tuv wx y zAB
    C DE F G HI J K L M NO 
</pre>

Matrix 3: Expectation: Should NOT be in 2. There is no capital Z in matrix 2.
<pre class="three">
    Z
   tuv
    J
</pre>

Matrix 4: Expectation: Should NOT be in 2. There is no capital Z in matrix 2.
<pre class="four">
    g
   Zuv
    J
</pre>

Matrix 5: Expectation: Should NOT be in 2. There is no capital Z in matrix 2.
<pre class="five">
    g
   tuv
    Z
</pre>

Matrix 6: Similar to Matrix 2, but g & J have left-shifted. "tuv" remain in place. "g" is no longer locked above "u" & "u" is no longer locked above "J". Expectation: Matrix 1 should NOT be in matrix 6.
<pre class="six">
    a b   g cdef  hi  j k l
    m no pqr s tuv wx y zAB
    C DE  J F G HI K L M NO 
</pre>

Matrix 7: Similar to Matrix 2, but g & "tuv" & J have left-shifted. The cross is still locked with "g" above "u" above "J". Expectation: Matrix 1 should be in matrix 7.
<pre class="seven">
    a b   g cdef  hi  j k l
    m no tuv pqr s wx y zAB
    C DE  J F G HI K L M NO 
</pre>

<div id="log"></div>

JavaScript

var debug = false;

function getMatrix(cssSelector, canTrim) {
	// Built by, Clomp! @briankueck http://www.clomp.com
	var obj = $(cssSelector);
	if (obj) {
		// Get the Text
		var matrix = obj.text()
		
		// Makes sure that we are selecting 3 lines, not 5 as the example has a \n after <div ...>\n and before \n</div>
		if (canTrim) {
			matrix = matrix.trim();
		}

		// Now split those 3 lines.
		matrix = matrix.split(/\n/);

		// Trims each array in the matrix.	
		// Note: matrix[row] is a string, but we can treat a string as an array of characters.
		if (canTrim) {
			// Trims each row, if desired.
			for (var row = 0; row < matrix.length; row++) {
				matrix[row] = matrix[row].trim();
			}
		} else {
			// Gets rid of spaces before matrix 1 in this demo.
			var maxLength = 0;
			var space = ' '; // You can also use a period here to see how it works.
			var tempMatrix = [];
			for (var row = 0; row < matrix.length; row++) {
				// log(matrix[row].length);

				// This cuts the rows down (vertically) from 5 to 3.
				if (matrix[row].trim().length > 0) {
					matrix[row] = matrix[row].replace(/\s/g, space);
					matrix[row] = matrix[row].replace(/\t/g, space);
					tempMatrix.push(matrix[row]);
					
					if (matrix[row].length > maxLength) {
						maxLength = matrix[row].length;
					}
				}
			}

			// This loops those 3 rows (horizontally) & slices the 1st character off each array if they are all identical & only contain spaces, which we are tracking with the period character '.' as dots.
			var charactersToStrip = 0;
			for (var column = 0; column <= maxLength; column++) {
				for (var row = 0; row < tempMatrix.length; row++) {
					if (tempMatrix[row][column] !== space) {
						break;
					} else if (row === (tempMatrix.length - 1)) {
						charactersToStrip++;
					}
				}
			}

			// Strips characters, without removing the space before "g" and the space before "J".
			for (var column = 0; column < charactersToStrip; column++) {
				for (var row = 0; row <...