Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 602899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:52:21+00:00 2026-05-13T16:52:21+00:00

i need to find few words or matching pattern using a Javascript. this is

  • 0

i need to find few words or matching pattern using a Javascript.

this is the requirement.

i have a string like this,

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

and i need to match this string against a string like this

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

how do i get the intersection of matching text blocks?

I already tried intersect Javascript script function, for some strings it’s not working properly.

How to solve this problem? can this be done using Regex?

Please advice.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T16:52:21+00:00Added an answer on May 13, 2026 at 4:52 pm

    You have to find the Longest common substring.

    If the strings are not very long, I recommend using Tim’s approach. Otherwise, this is a Javascript implementation of the Longest common substring algorithm with dynamic programming. The runtime is O(mn) where m and n are the lengths of the 2 strings respectively.

    An example usage:

    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 the algorithm implementation. It returns an array of the longest common substring(s). Extended the native String class, so the intersect method is available on 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;
    }
    

    Also need this helper function to create a 2d array with all elements initialize 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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 286k
  • Answers 286k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer As of version 5.6.5, Notepad++ supports JSP syntax highlighting! Hooray!… May 13, 2026 at 4:52 pm
  • Editorial Team
    Editorial Team added an answer You need to store blankViewController as an ivar of self,… May 13, 2026 at 4:52 pm
  • Editorial Team
    Editorial Team added an answer At my previous job all developers had a R:\ drive… May 13, 2026 at 4:52 pm

Related Questions

I frequently write throwaway code (in a research environment ) - for example to
I've got a tree view that can be anywhere from 1 level deep to
Wrote a small app that accesses a bunch of search websites and puts the
Our project has been using Git for a week or so now, and we're
I am trying to find some sort of gui or batch utility where i

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.