3

I want to check if a string (let entry) contains exact match with let expect:

let expect = 'i was sent'
let entry = 'i was sente to earth' // should return false
// let entry = 'to earth i was sent' should return true

// includes() using the first instance of the entry returns true
if(entry.includes(expect)){
console.log('exact match')

} else {
console.log('no matches')

}

There are lots of answers on StackOverflow but I can't find a working solution.

Note:

let expect = 'i was sent' 
let entry = 'to earth i was sent'

should return true

let expect = 'i was sent' 
let entry = 'i was sente to earth'

should return false

foxer
  • 811
  • 1
  • 6
  • 16
  • Do you want to see if it contains or is an exact match? – King11 Feb 27 '20 at 19:08
  • https://stackoverflow.com/a/447258/5132337 It gives the correct answer. – Siraj Alam Feb 27 '20 at 19:08
  • let expect = 'i was sent' let entry = 'i was sent e to earth' // should return false or true – Charlie Wallace Feb 27 '20 at 19:09
  • 1
    um so `entry === expect`? – epascarello Feb 27 '20 at 19:09
  • Does this answer your question? [Matching exact string with JavaScript](https://stackoverflow.com/questions/447250/matching-exact-string-with-javascript) – Siraj Alam Feb 27 '20 at 19:10
  • 1
    @foxer does "exact match" mean what it says, or would it just **containing** that string come back true also? I'm a bit confused....do you want to actually match a string exactly, or are you looking for the regex to identify that specific string somewhere in the string being passed as the arg? – Chris W. Feb 27 '20 at 19:12
  • I want to check if it containing the `expect` exactly, I think the `includes()` checks containing only that's why `let entry = 'i was sente to earth'` returns true unexpectedly... – foxer Feb 27 '20 at 19:16
  • Do you know what includes does exactly?? *update "The includes() method determines whether one string may be found within another string, returning true or false as appropriate."* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes So it is true because the substring exists inside of the string. – epascarello Feb 27 '20 at 19:20
  • Ok fair enough, updated. – Chris W. Feb 27 '20 at 19:26

2 Answers2

8

Seems like you're talking about matching word boundary, which can be accomplished using \b assertion in RegExp which matches word boundary, so there you go:

const escapeRegExpMatch = function(s) {
    return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
};
const isExactMatch = (str, match) => {
  return new RegExp(`\\b${escapeRegExpMatch(match)}\\b`).test(str)
}

const expect = 'i was sent'

console.log(isExactMatch('i was sente to earth', expect)) // <~ false
console.log(isExactMatch('to earth i was sent', expect)) // <~ true

Hope it helps :)

Max
  • 1,996
  • 1
  • 10
  • 16
  • Hello, this fails when string contains {([ for example : isExactMatch("09.12.06.Inkigayo.f(x) - Chu.tp", "f(x)") // false – AhmedO Sep 01 '22 at 01:15
2

I have not tested this but you need to convert the expected to an array of strings and then check if all items are in the entry string.

let arr = expect.split(" ");
if (arr.every(item => entry.includes(item)) {
  console.log("match");
}
else.....
taylorj94
  • 71
  • 3