look for the similarity of words in sentences javascript

I am trying to find the similarity between the sentences in javascript, example :

let mastersentences = "Gambir, Kecamatan Gambir, Kota Jakarta Pusat, Daerah Khusus Ibukota Jakarta";
let keywordsentences = "DKI JAKARTA";
let result = ( mastersentences  === keywordsentences ) // true because 'jakarta' is match

the result of equating ‘mastersentence’ and ‘keywordsentence’ is "jakarta", i have try using more method of javascript but i still can’t find the result like use :

includes()
indexOf()
lastIndexOf()
localeCompare()
match()
search()

>Solution :

You can split each sentence by \W+ which any character isn’t text or number or underscore, and use some method, to check if there are any matches after converting each work to lower case to prevent sensitivity.

let mastersentences = "Gambir, Kecamatan Gambir, Kota Jakarta Pusat, Daerah Khusus Ibukota Jakarta";
let keywordsentences = "DKI JAKARTA";

const result = mastersentences.split(/\W+/).some(w => keywordsentences.split(/\W+/).some(k => k.toLowerCase() === w.toLowerCase()))

console.log(result)

Leave a Reply