How to check if a string contains a specific word.

Hi,

I’m looking to do the same thing .contains() does, but for a string.

I have a string called bum which can be any possible string as it is obtained by a user typing it in.

I then want to check if bum contains math or bio etc…

I’ve tried to use bum.contains(), but it’s not a function. Is there some function I can find that would do this? Because as you can see below, my code will be a mess if I have to type every possible iteration of each likely word.


import wixData from 'wix-data';
import { memory } from 'wix-storage';

let bum = memory.getItem("Subject");

$w.onReady(function () {
    $w("#teamDataset").setSort( wixData.sort()
  .descending("views"))
    if (memory.getItem("Foreign") == "Foreign Languages") {
        $w("#searchbar1").value = "Foreign Languages";
    } else {
        $w("#searchbar1").value = bum;
        $w("#searchbar1").collapse();
    }
    if (bum != "All" && bum != null && bum != "") {
        filter(bum);
    } else {
        $w("#teamDataset").setFilter(wixData.filter().contains("courses", ""));
        $w("#searchbar1").value = "All";
    }
})

export function searchbar1_change(event) {

    let bum = $w("#searchbar1").value;
    if (bum != "All" && bum != null && bum != "") {
        $w("#dropdown2").collapse();
        filter(bum);

    } else {

        $w("#teamDataset").setFilter(wixData.filter().contains("courses", ""))
        $w("#searchbar1").value = "All";
        $w("#dropdown2").collapse();
    }
}

export function dropdown2_change(event) {

    let bum = $w("#dropdown2").value;
    if (bum == "English") {
        $w("#teamDataset").setFilter(wixData.filter().contains("courses", "English,"));
    } else {
        $w("#teamDataset").setFilter(wixData.filter().contains("courses", bum));
    }
}
export function filter(bum) {
    if (bum == "Foreign Languages") {
        $w("#dropdown2").expand();
        $w("#dropdown2").value = bum;
    } else {
        $w("#dropdown2").collapse()
        if (bum == "English Literature") {
            $w("#teamDataset").setFilter(wixData.filter().contains("courses", "Lit"));
        } else if (bum == "English Language") {
            $w("#teamDataset").setFilter(wixData.filter().contains("courses", "Lang"));
        } else if (bum == "math" || bum == "Math" || bum == "MATH" || bum == "maths" || bum == "Maths" || bum == "MATHS" || bum == "mathematics" || bum == "Mathematics" || bum == "MATHEMATICS") {
            $w("#teamDataset").setFilter(wixData.filter().contains("courses", "Math"));
        } else {
            $w("#teamDataset").setFilter(wixData.filter().contains("courses", bum));
        }
    }
}

you can use RegEx in combination with → match()…

var str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
var regexp = /[A-E]/gi;
var matches_array = str.match(regexp);

console.log(matches_array);// ['A', 'B', 'C', 'D', 'E', 'a', 'b', 'c', 'd', 'e']

Here’s a snippet of code which will tell you if any word in a list of words appears in a text string:

const words = [
    "beer",
    "stout",
    "pilsner",
    "lager"
];

let text = "this is a string of text that has a gratuitous mention of stout";
let rgx = new RegExp(words.join("\\b|\\b"), "i");
let ret = rgx.test(text);
console.log(ret); // should display true since the string has the word "stout" in it

You can make changes to the regular expression to better suit your needs.

2 Likes

Alternatively you can also do something like:

let string = 'How are you';
let word = 'are';
let matchCheck = string.includes(word);//true
2 Likes