To Check If String Contains an Exact Match in JavaScript, we use 2 methods where we use two main JavaScript built-in functions. One is split()
and second is RegExp
method.
Here are 2 steps to Check If String Contains Exact Match using JS,
- Using RegExp with replace() function.
- Using split() with for loop.
Method-1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<script type="text/javascript"> function ContainsExactWord(sentence, compare) { var words = sentence.split(" "); for (var i = 0; i < words.length; ++i) { if(words[i] === compare) { alert("found " + compare); break; } } alert("Could not find the word"); } $(function() { var sentence = "Lorem ipsum dolor sit amet, consectetur adipisicing elit"; var keywordOne = "Lorem"; var keywordTwo = "LoremIpsum"; $('#execute').bind('click', function(undefined) { ContainsExactWord(sentence, keywordOne); ContainsExactWord(sentence, keywordTwo); }); }); </script> |
Live View:
https://jsfiddle.net/PHPCODERTECH/nzd7etcr/
Code Explanations:
- Here we create variables to store some strings and words first.
- After that, we split all the words from the complete sentence using var
words = sentence.split(" ");
- At the last, we take all the words from sentences using the loop and compare them with the given string.
for (var i = 0; i < words.length; ++i) {
if(words[i] === compare) {
alert("found " + compare);
break;
}
}
- Now you can call the created function with required parameters.
Method-2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<script type="text/javascript"> // method 2 var sentence = "Lorem ipsum dolor sit amet, consectetur adipisicing elit"; var keyword = "Lorem"; const escapeRegExpMatch = function(s) { return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'); }; const isExactMatch = (str, match) => { return new RegExp(`\\b${escapeRegExpMatch(match)}\\b`).test(str) } if (isExactMatch(sentence, keyword) == true) { alert("Matched"); }else{ alert("Not Matched"); } </script> |
Live View:
jsfiddle.net/PHPCODERTECH/fk3y207v/
Code Explanations:
- In this method, first, we replace all the spaces and other slashes from the string using replace() JS function.
replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')
- In the second step, we use regExp or regular expression to check the exact match. Also, we use the above replace functionality inside the regExp object.
return new RegExp(
\\b${escapeRegExpMatch(match)}\\b
).test(str)
- At last, we match the string with a sentence using the created function.
isExactMatch(sentence, keyword)
Here is complete solution of JavaScript String Contains Exact Match.
you can check live view and modify according to you. To know more about regular expression in JavaScript you can check here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp
Also Check:
- 2 Ways to Open URL in New Tab Using JavaScript
- How to Embed PDF in Website Using HTML
- Check If Folder Or File Exists Using JavaScript
- How to Open an External URL in PHP
Happy Coding..!
[…] Check If String Contains Exact Match in JavaScript […]
[…] Also Read: Check If String Contains Exact Match in JavaScript […]