0

I have an array which contains words. Also i have a string. I need to check whether an element of array exists in a string. I've tried but without success. It's not working.

function inputValidate() {
    var val = document.getElementById("title");
    var keyWords = ["kerak", "nega", "qanday", "qanaqa", "nimaga", "mi"];
    val.value.trim();
    len = val.value.length - 1;
    lastS = val.value.slice(len);

    if (lastS != "?") {
        document.getElementById("error").innerHTML = "Savol so`roq belgisi bilan tugashi lozim.";
    } else {
        document.getElementById("error").innerHTML = " ";
        document.getElementById("error").style.color = "red";
    }

    for (i = 0; i < keyWords.length; i++) {
        if (val.indexOf(keyWords[i]) != -1) {
            document.getElementById("error2").innerHTML = "Gapingizga so`roq gapga o`xshamadi ";
        } else {
            document.getElementById("error2").innerHTML = " ";
        }
    }
}

http://codepen.io/anon/pen/vOMWyQ

9
  • I've already used it. Check the code Commented Aug 13, 2015 at 15:00
  • what is the value in title? Also you will end up matching words inside of words. Commented Aug 13, 2015 at 15:01
  • My bad, still i = 6 instead of i<keyWords.length is an error Commented Aug 13, 2015 at 15:03
  • a sentence. It's a string. Commented Aug 13, 2015 at 15:03
  • 1
    @DalHundal Many edits ago val was a DOM node Commented Aug 13, 2015 at 15:08

1 Answer 1

1

I need to check whether an element of array exists in a string.

// The elements you want to be checked in a certain string.
var KEYWORDS = [ 'world' ];

/**
 * Checks if an element of a keyword array occurs in a certain text string.
 *
 * @param {Array}  keywords   - contains keyword strings
 * @param {String} textString - text string to be checked
 *
 * @return {Boolean} denotes if a match was found.
 */
var keywordExistsInString = function (keywords, textString) {
    // Split the text string for easy matching.
    var words = textString.slice(/\s*\b\s*/);

    // Only interested if ONE of the keywords matches.
    // NOTE: if all keywords must match use 'every()' instead of 'some()'.
    return keywords.some(function (keyword) {

        // Use 'bitwise not' to determine a match.
        // Double negate to convert to a Boolean.
        return !!~words.indexOf(keyword);
    });
};

keywordExistsInString(KEYWORDS, 'hello world'); // true
keywordExistsInString(KEYWORDS, 'hello');       // false
Sign up to request clarification or add additional context in comments.

4 Comments

what does (/\s*\b\s*/) mean? Could you explain please
@Sardor \b splits on word boundaries and \s* matches zero or more white space characters, including space, tab, form feed, line feed. For more info check MDN
Why the "cryptic" return !!~words.indexOf(keyword) instead of simply return words.indexOf(keyword) > -1
@Andreas in my opinion the ~ operator is more elegant than > 1. The double negation is shorter that using Boolean(). But on the other hand, when using > 1, the conversion to a Boolean does become superfluous. I guess you might be right that it could be a little bit cryptic though, especially when not dropping comments. I guess it's a matter of taste :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.