Javascript String Template Help

I need to find some words or a matching pattern using Javascript.

this is a requirement.

I have a line like this,

Here is a quick guide for the next time you will reach your favorite butter and some other topics.

and I need to match this line with a line like this

favorite oil and some other topics can be based on something blah blah 

How do I get the intersection of the corresponding text blocks?

I already tried traversing the Javascript script function, as some lines are not working properly.

How to solve this problem? can this be done with regex?

Please advice.

+6
javascript string pattern-matching
source share
3 answers

You should find the longest common substring .

If the lines are not very long, I recommend using Tim's approach. Otherwise, it is a Javascript implementation of the longest general substring algorithm with dynamic programming. The runtime is O (mn), where m and n are the lengths of two lines, respectively.

Usage example:

 var first = "Here is a quick guide for the next time you reach for your favorite oil and some other topics"; var second = "favorite oil and some other topics can be based on something blah blah"; console.log(first.intersection(second)); // ["favorite oil and some other topic"] 

This is an implementation of the algorithm. It returns an array of the longest common substring. Made its own String class, so the intersect method is available for all strings.

 String.prototype.intersection = function(anotherString) { var grid = createGrid(this.length, anotherString.length); var longestSoFar = 0; var matches = []; for(var i = 0; i < this.length; i++) { for(var j = 0; j < anotherString.length; j++) { if(this.charAt(i) == anotherString.charAt(j)) { if(i == 0 || j == 0) { grid[i][j] = 1; } else { grid[i][j] = grid[i-1][j-1] + 1; } if(grid[i][j] > longestSoFar) { longestSoFar = grid[i][j]; matches = []; } if(grid[i][j] == longestSoFar) { var match = this.substring(i - longestSoFar + 1, i); matches.push(match); } } } } return matches; } 

You also need this helper function to create a 2d array with all initialized elements up to 0.

 // create a 2d array function createGrid(rows, columns) { var grid = new Array(rows); for(var i = 0; i < rows; i++) { grid[i] = new Array(columns); for(var j = 0; j < columns; j++) { grid[i][j] = 0; } } return grid; } 
+8
source share

This is not very efficient, and there are much better ways to do this at all (see @Anurag answer), but it is simple and works fine for short lines:

 function stringIntersection(str1, str2) { var strTemp; // Swap parameters if necessary to ensure str1 is the shorter if (str1.length > str2.length) { strTemp = str1; str1 = str2; str2 = strTemp; } // Start with the whole of str1 and try shorter substrings until // we have a common one var str1Len = str1.length, l = str1Len, start, substring; while (l > 0) { start = str1Len - l; while (start >= 0) { substring = str1.slice(start, l); if (str2.indexOf(substring) > -1) { return substring; } start--; } l--; } return ""; } var s1 = "Here is a quick guide for the next time you reach" + " for your favorite oil and some other topics"; var s2 = "favorite oil and some other topics can be based on" + " something blah blah"; alert( stringIntersection(s1, s2) ); 
+3
source share

Simple polyfill filter string

 if (!String.prototype.intersection) { String.prototype.intersection = function(anotherString, caseInsensitive = false) { const value = (caseInsensitive) ? this.toLowerCase() : this; const comp = (caseInsensitive) ? anotherString.toLowerCase() : anotherString; const ruleArray = comp.split("").reduce((m,v) => {m[v]=true; return m;} ,{}) return this.split("").filter( (c, i) => ruleArray[value[i]] ).join("") } } 

"HelloWorld" .intersection ("HEWOLRLLODo", true)

"HelloWorld" - case insensitive

"HelloWorld" .intersection ("HEWOLRLLODo")

"HoWo" - case sensitive

0
source share

All Articles