0

I am trying to match word in javascript if i use 'if' and 'split' method each time I stuck in the case-sensetive.

If I am using regExp then if a part of word matched it returned true (i.e : hii , /hi/). What I do do to match case-insensitive and whole word should be match.

Code of the regEx

async function matchOne(str ,mtc) {
  let returnval;
  let words = str.split(' ')
  await words.forEach(word => {
    if (word.match(mtc)) {
      returnval = true;
      return true
    }
  });
  if (returnval === true) {
    return true;
  } else {
    return false;
  }
}
matchOne(string,regEx)

Code of the if statement

async function matchOne(str ,mtc) {
  let returnval;
  let words = str.split(' ')
  await words.forEach(word => {
    if (word == mtc) {
      returnval = true;
      return true
    }
  });
  if (returnval === true) {
    return true;
  } else {
    return false;
  }
}

matchOne(string,string)
Ali Asgher Badshah
  • 811
  • 1
  • 11
  • 34
Anurag Kumar
  • 129
  • 1
  • 7

4 Answers4

0

Hey there check out the code snippet below hopefully that should help you

const str = 'arya stark';

// The most concise way to check substrings ignoring case is using
// `String#match()` and a case-insensitive regular expression (the 'i')
str.match(/Stark/i); // true
str.match(/Snow/i); // false

In case you want to use forEach then consider converting the string to lowercase and search for a lowercase version of the word as shown below

str.toLowerCase().includes('Stark'.toLowerCase()); // true
str.toLowerCase().indexOf('Stark'.toLowerCase()) !== -1; // true
Yash.S.Narang
  • 518
  • 4
  • 13
  • `What I do do to match case-insensitive and whole word should be match.` Can this also solve for whole words? Example `the boy spoke starkly` should not match. – stealththeninja Jul 25 '20 at 03:12
0

I think this will work for you.

I have removed some code for clarity.

function matchOne(str ,mtc) {
  let returnval;
  let words = str.split(' ')
  words.forEach(word => {
    console.log(word)
    if (word.toLower().match(mtc)) {
      console.log('Found word:'+ word + ' regexp='+mtc)
      returnval = true;
    } else console.log('NOT Found word:'+ word  + ' regexp='+mtc)
  });
  return returnval;
}
matchOne('Hello world','he'); // true
matchOne('Hello world','He'); // true

As for Upper/Lower case. You need to decide if input & regexp should be upper or lower or whatever. Just append .toLower() to your argument.

Example: word.toLower().match(mtc)

Dharman
  • 30,962
  • 25
  • 85
  • 135
Mulli
  • 1,598
  • 2
  • 22
  • 39
0

You can match a string with regexp and use the global and case-insensitive flags

let str = 'this string have Capital and no capital';

let match = str.match(/capital/gi);

console.log(match);
sonEtLumiere
  • 4,461
  • 3
  • 8
  • 35
0
  1. Regex matching

Considering that regex will match the whole string, splitting the string into words is not necessary.

const regex = /hello/gi
const string = "hello world"
console.log(!!string.toLowerCase().match(regex)); // true

const string2 = "HELLO WORLD"
console.log(!!string2.toLowerCase().match(regex)); // true

const string3 = "WORLD ABC"
console.log(!!string3.toLowerCase().match(regex)); // false

Here i makes the regex matching case insensitive. !! is used to convert the array we receive after matching to a boolean.

  1. String matching

You can simplify and clean up your code further - something like this. We split the string by words. This will give us an array of words. Now we check if at least one word equates to your match.

function matchOne(string, mtc) {
  return string.split(' ').some(word => word.toLowerCase() === mtc.toLowerCase())
}

console.log('hello world', 'hello'); // true
console.log('world abc', 'hello'); // false

drtechie
  • 81
  • 2
  • 7
  • Also some comments about your code snippet. 1. `async-await` is not needed here for forEach. 2. There is no need to return a value within `forEach`. In such a context, use `map`. Read more here:https://stackoverflow.com/questions/34426458/javascript-difference-between-foreach-and-map – drtechie Jul 25 '20 at 05:29
  • I added return value beacause I just want to stop the loop if condition match. Here I am using split method if the last word not matched then it set the value of returnval to false. And I dont want it so i do that to stop the loop – Anurag Kumar Jul 25 '20 at 12:56
  • I think It will work:- `async function matchOne(str ,mtc) { let returnval; let words = str.split(' ') await words.forEach(word => { if (word.toLowerCase() == mtc.toLowerCase()) { returnval = true; return true; } }); if (returnval === true) { return true; } else { return false; } } matchOne(string,string)` – Anurag Kumar Jul 25 '20 at 13:02
  • " I just want to stop the loop if condition match" => you can't break a forEach. Read more: https://stackoverflow.com/questions/6260756/how-to-stop-javascript-foreach – drtechie Jul 25 '20 at 17:56