Checking the presence of multiple words in a variable using JavaScript - javascript

The code the presence of a single word in a sentence and it's working fine.
var str ="My best food is beans and plantain. Yam is also good but I prefer yam porrage"
if(str.match(/(^|\W)food($|\W)/)) {
alert('Word Match');
//alert(' The matched word is' +matched_word);
}else {
alert('Word not found');
}
Here is my issue: I need to check presence of multiple words in a sentence (eg: food,beans,plantains etc) and then also alert the matched word.
something like //alert(' The matched word is' +matched_word);
I guess I have to passed the searched words in an array as per below:
var words_checked = ["food", "beans", "plantain"];

You can construct a regular expression by joining the array of words by |, then surround it with word boundaries \b:
var words_checked = ['foo', 'bar', 'baz']
const pattern = new RegExp(String.raw`\b(?:${words_checked.join('|')})\b`);
var str = 'fooNotAStandaloneWord baz something';
console.log('Match:', str.match(pattern)[0]);

Here's a way to solve this. Simply loop through the list of words to check, build the regex as you go and check to see if there is a match. You can read up on how to build Regexp objects here
var str ="My best food is beans and plantain. Yam is also good but I prefer
yam porrage"
var words = [
"food",
"beans",
"plantain",
"potato"
]
for (let word of words) {
let regex = new RegExp(`(^|\\W)${word}($|\\W)`)
if (str.match(regex)) {
console.log(`The matched word is ${word}`);
} else {
console.log('Word not found');
}
}

var text = "I am happy, We all are happy";
var count = countOccurences(text, "happy");
// count will return 2
//I am passing the whole line and also the word for which i want to find the number of occurences
// The code splits the string and find the length of the word
function countOccurences(string, word){
string.split(word).length - 1;
}

Related

Search mechanism to include words as a whole [duplicate]

This question already has answers here:
Regex match entire words only
(7 answers)
Closed 4 years ago.
I have created a search mechanism that searches through an array of strings for an exact string match, however I want it to be a bit more intuitive.
I can also get it to search for a string within the string (for example chicken in grilled chicken - however the issue is this allows users to type ken or ill and it returns grilled chicken.
I would like it to return if I typed in chicken or grilled.
Does anyone have any suggestions on how to have a more intuitive search mechanism?
EDIT:
The correct answer below worked when typing 1 word and it would search all individual words in a string. However, I realised it fails when you search with 2 words (as it only searches each string word individually).
I solved this by adding || search == string to the if to include not just individually word matches but whole string matches.
However I am still having an issue with it either searching for:
Whole string matches
OR
Matches with individual words.
This means it fails when search = green cup and string = big green cup. Is there a way to solve this by cutting for collections to search within? Perhaps something similar to:
string.split(' ') but to also include big green, green cup to the array also?
Try This Simplest Code without Regex
var data = ["first string1 is here", "second string2 is here", "third string3 is here"];
var wordToSearch = "string2 is thanks";
var broken = wordToSearch.split(' ');
var result = 'not found';
if(broken.length == 1){
data.forEach(function(d){
d1 = d.split(' ');
if(d1.includes(wordToSearch))
result = d;
});
}
else if(broken.length == 2)
{
data.forEach(function(d){
var d1 = d.split(' ');
if(d1.includes(broken[0]) && d1.includes(broken[1]))
{
result = d;
}
});
}
alert(result);
I'd use RegExp with word boundary anchor - \b.
function search(query, arr) {
var res = [];
var re = new RegExp('\\b' + query + '\\b');
arr.forEach(function (item) {
if (re.test(item)) res.push(item);
});
return res;
}
It sounds like you only want to search by whole words, if that's the case, you could split the string by the space character and then search through the resultant array for matches.

How can I split commas and periods from words inside of string using split?

I am trying to change specific word in a string with something else. For example, I want to change 'John' in let name = 'Hi, my name is John.'; to 'Jack'.
I know how to split a string by words or characters. I also know how to remove commas, periods, and other symbols in a string. However, if I split the given string with a separator (" "), I will have 'John.' which I do not want. (I know I can switch 'John.' with 'Jack.' but assume that I have an key and value pairs in an object and I am using the values which are names {Father: Jack, Mother: Susan, ...}
I don't know how to separate a string word by word including commas and periods.
For example, if I was given an input which is a string:
'Hi, my name is John.'
I want to split the input as below:
['Hi', ',', 'my', 'name', 'is', 'John', '.']
Does anyone know how to do it?
Below is the challenge I am working on.
Create a function censor that accepts no arguments. censor will return a function that will accept either two strings, or one string. When two strings are given, the returned function will hold onto the two strings as a pair, for future use. When one string is given, the returned function will return the same string, except all instances of a first string (of a saved pair) will be replaced with the second string (of a saved pair).
//Your code here
const changeScene = censor();
changeScene('dogs', 'cats');
changeScene('quick', 'slow');
console.log(changeScene('The quick, brown fox jumps over the lazy dogs.')); // should log: 'The slow, brown fox jumps over the lazy cats.'
I think your real question is "How do I replace a substring with another string?"
Checkout the replace method:
let inputString = "Hi, my name is John.";
let switch1 = ["John", "Jack"];
let switched = inputString.replace(switch1[0], switch1[1]);
console.log(switched); // Hi, my name is Jack.
UPDATE: If you want to get ALL occurrences (g), be case insensitive (i), and use boundaries so that it isn't a word within another word (\\b), you can use RegExp:
let inputString = "I'm John, or johnny, but I prefer john.";
let switch1 = ["John", "Jack"];
let re = new RegExp(`\\b${switch1[0]}\\b`, 'gi');
console.log(inputString.replace(re, switch1[1])); // I'm Jack, or johnny, but I prefer Jack.
You can Try This ...
var string = 'Hi, my name is John.';
//var arr = string.split(/,|\.| /);
var arr = string.split(/([,.\s])/);
console.log(arr);
Using 'Hi, my name is John.'.split(/[,. ]/); will do the job. It will split commas and periods and spaces.
Edit: For those who want to keep the comma and period, here is my wildly inefficient method.
var str = 'Hi, my name is John.'
str = str.replace('.', 'period');
str = str.replace(',', 'comma');
str = str.split(/[,. ]/);
for (var i = 0; i < str.length; i++) {
if (str[i].indexOf('period') > -1) {
str[i] = str[i].replace('period', '');
str.splice(i+1, 0, ".");
} else if (str[i].indexOf('comma') > -1) {
str[i] = str[i].replace('comma', '');
str.splice(i+1, 0, ",");
}
}
console.log(str);

Find number of replacements when using a global Regular Expression

I have a sentence (string) containing words. I want to replace all occurrences of a word with another. I use newString = oldString.replace(/w1/gi, w2);, but now I need to report to the user how many words I actually replaced.
Is there a quick way to do it without resorting to:
Replacing one word at a time and counting.
Comparing oldString to newString word-by-word and tallying the differences?
(The easy case is if oldString === newString => 0 replacements, but beyond that, I'll have to run over both and compare).
Is there any RegEx "trickery" I can use here, or should I just avoid using the g flag?
Option 1: Using the replace callback
By using the callback, you can increment a counter, and then return the new word in the callback, this allows you to traverse the string only 1 time, and achieve a count.
var string = 'Hello, hello, hello, this is so awesome';
var count = 0;
string = string.replace(/hello/gi, function() {
count++;
return 'hi';
});
console.log('New string:', string);
console.log('Words replaced', count);
Option 2: Using split and join
Also using the split method, instead of using regex, just join with the new word to create the new string. This solution allows you to avoid using regex at all to achieve counts.
var string = 'Hello, hello, hello, this is so awesome';
string = string.split(/hello/i);
var count = string.length - 1;
string = string.join('Hi');
console.log('New string:', string);
console.log('Words replaced', count);
You could split the string with the regex you're using and get the length.
oldString.split(/w1/gi).length - 1
Working example:
var string = "The is the of the and the";
var newString = string.replace(/the/gi, "hello");
var wordsReplaced = string.split(/the/gi).length - 1;
console.log("Words replaced: ", wordsReplaced);

Javascript regular expression for matching whole words including special characters

I am trying to match whole exact words using a javascript regular expression.
Given the strings: 1) "I know C++." and 2) "I know Java."
I have tried using new Regex('\\b' + text + '\\b', 'gi') and that works great for words without special characters like example #2.
I've also taken a look at this url:
Regular expression for matching exact word affect the special character matching
and implemented the:
escaped = escaped.replace(/^(\w)/, "\\b$1");
escaped = escaped.replace(/(\w)$/, "$1\\b");
and that will match text = 'C++' (it will match both examples)
However, if someone types a typo, and the string is "I know C++too.", the latter regex will still match the C++ when I don't want it to because the word "C++too" is not an exact match for text = 'C++'.
What changes can I make so that it will not match unless C++ is both the front of the word and the end of the word.
You can add a range of accepted characters([+#]) after word characters:
str = 'I know C++too. I know Java and C#.';
console.log(str.match(/(\w[+#]+|\w+)/g));
NB: \w[+#]+ must be placed first in the alternation expression to take precedence over the more generic \w+.
If whole words including special characters means everything but [\r\n\t\f\v ], you can simply do:
const REGEX = /([^\s]+)+/g;
function selectWords(string) {
const REGEX = /([^\s]+)+/g;
return string
// remove punctuation
.replace(/[^a-z0-9\s+#]/ig, "")
// perform the match
.match(REGEX)
// prevent null returns
|| []
;
}
var text = "Hello World"
var [first, second, ...rest] = selectWords(text);
console.log(1, {first, second, rest});
// example with punctuation
var text = "I can come today, she said, but not tomorrow."
var [first, second, third, ...rest] = selectWords(text);
console.log(2, {first, second, third, rest});
// example with possible throw
var text = ",.'\"` \r"
var [first, second, third, ...rest] = selectWords(text);
console.log(3, {first, second, third, rest});
// example with a specific word to be matched
function selectSpecificWord(string, ...words) {
return selectWords(string)
.filter(word => ~words.indexOf(word))
;
}
var expected = "C++";
var test = "I know C++";
var test1 = "I know C++AndJava";
console.log("Test Case 1", selectSpecificWord(test, expected));
console.log("Test Case 2", selectSpecificWord(test1, expected));
Use this ((?:(?:\w)+?)(?=\b|\w[-+]{2,2})(?:[-+]{2,2})?)
I've included a - symbol for an example also. See it in life.

spliting text into array in javascript

I want to split when getting , or . in text in javascript.
My text is like this:
The cats climbed the tall tree.In this sentence, $U_SEL{} is a noun.
I want array as:
1.The cats climbed the tall tree.
2.In this sentence
3.$U_SEL{}
4.is a noun
try this
<script type="text/javascript">
var text = "The cats climbed the tall tree.In this sentence, $U_SEL{}, is a noun";
var spliteds = text.split(/[\.,]/);
alert(spliteds[0]);
alert(spliteds[1]);
alert(spliteds[2]);
alert(spliteds[3]);
</script>
The regular expression for this challenge will be.
var text = "The cats climbed the tall tree.In this sentence, $U_SEL{} is a noun."
var regex = /[.,]/;
text.split(regex);
FOR MORE INFORMATION ABOUT regex VISIT
https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions
Here is the regex. To split on {} first replace that with {}, or {}., then try split.
var str = "The cats climbed the tall tree.In this sentence, $U_SEL{} is a noun";
str = str.replace("{}", "{},");
//Array with splitted value
var result = str.split(/[,.]/g);
//Printing the result array
console.log(result);
'The cats climbed the tall tree.In this sentence, $U_SEL{} is a noun.'.split(/[\.,]/)
would return:
Array [ "The cats climbed the tall tree", "In this sentence", " $U_SEL{} is a noun", "" ]
Take a look at String.prototype.split()
A Regular Expression is your best option in this case. All the above posts are already correctly covering this way of solving your issue. I am just leaving here another approach that will provide what you are after if you have no idea of how Regular Expressions work.
Take into account though that RegExp is quite optimal choice in your scenario. The above code is mostly to show how it can be done without using RegExps. (Not to mention that it will get chaotic adding more delimiters)
var myString = "The cats climbed the tall tree.In this sentence, $U_SEL{} , is a noun";
var mySplitString = myString.split(",");
var myFinalArray = new Array();
mySplitString.forEach(function(part){
var myTemp = part.split(".");
myTemp.forEach(function(key){
myFinalArray.push(key);
});
});
console.log(myFinalArray);
Maybe split is not accurate because splitting requires a single character delimiter and there is no delimiter for the third element.
Trying capturing rather than splitting may work better (though I don't know if it is wise from performance point of view).
You could try this:
var pattern = /(([^.,]+?)([.,]|\{\})) */g;
var captures = [];
var s = 'First capture.Second capture, $THIRD_CAPTURE{} fourth capture.';
while ( (match = pattern.exec(s)) != null ) {
if (match[3] == "." || match[3] == ",") {
captures.push(match[2]);
} else {
captures.push(match[1]);
}
}
console.log(captures);
var captures = [];
var s = 'The cats climbed the tall tree.In this sentence, $U_SEL{} is a noun.';
while ( (match = pattern.exec(s)) != null ) {
if (match[3] == "." || match[3] == ",") {
captures.push(match[2]);
} else {
captures.push(match[1]);
}
}
console.log(captures);
The principle is as below.
Capture blocks of either a part of the sentence ended by a dot or a comma, without inner dot or comma, or ending with empty brackets pair
Within each block, capture both the content and the ending (either a dot, a comma or an empty brackets pair)
For each resulting match, you have three captures:
At index 1, the first block
At index 3, the ending
At index 2, the content without the ending
Then, according to the ending, either the match of idx 1 or 2 is stored.
You could modify the loop selecting the match to get exactly what you want, with the dot on the first capture and not on the last one, unless it is a typo.

Categories

Resources