-2

Let's say I have this string:

const string1 = "Qualification Level"

..and I want to compare it to a few different strings that have 'roughly' the same value or at least contains some resemblance:

const string2 = "Qualification" // just one of the words
const string3 = "level" // just one of the words and lowercase
const string4 = "qualification level" // lowercase

What is the best algorithm/regex expression to use so that comparing string1 to any of the strings I mentioned will return true?

catandmouse
  • 11,309
  • 23
  • 92
  • 150
  • What is the range of the words to match? – Maheer Ali May 09 '19 at 09:29
  • 1
    I don't know if this question belongs here on SO but this sounds like some heuristic selection problem. Maybe lowercase both strings, then use the longest common subsequence algorithm, then maybe divide by the length of the longer string to get a "similarity %"? – T Tse May 09 '19 at 09:30
  • https://stackoverflow.com/questions/473522/word-comparison-algorithm – str May 09 '19 at 09:30
  • There seems to be quite a few hits if you search for "string similarity algorithm" on google – T Tse May 09 '19 at 09:31
  • `string1.toLowerCase().includes(search.toLowerCase())` ? Works for all three example search texts you mentioned – adiga May 09 '19 at 09:34

2 Answers2

1

There are several string or phonetic similarity algorithms that you could use for that. For example:

Depending on what exactly you want to do, those are not even necessary though. Just make all your strings lowercase and split it into individual words. Then use Array#includes to check whether a certain word is contained in string1.

str
  • 42,689
  • 17
  • 109
  • 127
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/22966673) – Ankh May 09 '19 at 09:55
  • 1
    @Ankh The links go either to articles on Wikipedia or to possible implementations of the algorithms. Wikipedia will not go anywhere and I won't copy the implementation here as they might become outdated if the source changes. – str May 09 '19 at 09:59
-1

The string.match() is an inbuilt function in JavaScript which is used to search a string for a match against a any regular expression and if the match will found then this will return the match as an array. Syntax:

stirng.match(regExp)

Example:

Input:

var string = Welcome to geeks for geeks!
document.write(string.match(/eek/g);

Output:

eek, eek

Here “g” flag indicates that the regular expression should be tested against all possible matches in a string.