1

I have this 2 strings:

var test = 'BN123';
var behaviour = 'BN***,TA****';

I need to check if behaviour contains a string with the same format as test.

On the behaviour, the BN and TA as to be equal, and the * means it can be any char. (behaviour comes from an API, so I never know what it has, this is just a test.)

In this case it should return true. Right now I'm only comparing is case behaviour as a single string, but I need to modify that:

isValidInput(behaviour, test) {
  if (behaviour.length != test.length) {
    return false;
  }
  for (var i = 0; i < behaviour.length; i++) {
    if (behaviour.charAt(i) == '*') {
      continue;
    }
    if (behaviour.charAt(i) != test.charAt(i)) {
      return false;
    }
  }
  return true;
}
6
  • 1
    Can you add some expected input/output? Commented Jul 17, 2018 at 12:40
  • Dude! you can not use 'string' as a variable name. It's a reserved keyword in JS like var, object, class or what have you. Commented Jul 17, 2018 at 12:45
  • 2
    @UtkarshPramodGupta it is not. I'd also advise against using "string" as an identifier though. Commented Jul 17, 2018 at 12:46
  • well, changed string to test :p Commented Jul 17, 2018 at 12:47
  • 1
    You need regex. I kind of suck at it but I ended up with ^((TA(\w)*)|(BA(\w)*)). You can try to improve that in regex101.com . Also note that it is way faster to compare to ONE big regex than to compare many times with tiny regex Commented Jul 17, 2018 at 12:48

6 Answers 6

2

You can use .some() of Array.prototype.

like below

function isValidInput(behaviour, string1) {
    if (behaviour.length != string1.length) {
      return false;
    }

    for (var i = 0; i < behaviour.length; i++) {
      if (behaviour.charAt(i) == '*') {
        continue;
      }

      if (behaviour.charAt(i) != string1.charAt(i)) {
        return false;
      }
    }

    return true;
  }
  
var test = 'BN123';
var behaviour = 'BN***,TA****';

console.log(behaviour.split(',').some(x => isValidInput(x,test)));
console.log(behaviour.split(',').some(x => isValidInput(x,"test")));

Sign up to request clarification or add additional context in comments.

1 Comment

As I said in another answer, this seems the correct and "easier" way of doing it. I'm accepting this one as I am getting an error on the other one, although I know its correct too :p Thank you so much!
2

The only issue I see with your implementation is that you're not allowing for the fact behaviour contains possible strings separated with a comma (or at least, that's how it looks to me). So you need to check each of them:

// Check one behaviour string from the list
function isOneValidInput(behaviour, string) {
  if (behaviour.length != string.length) {
    return false;
  }

  for (var i = 0; i < behaviour.length; i++) {
    // Note we can easily combine those conditions, and use []
    // with strings
    if (behaviour[i] != '*' && behaviour[i] != string[i]) {
      return false;
    }
  }

  return true;
}

// Check all behaviour strings in a comma-separated one
function isValidInput(behaviours, string) {
  return behaviours.split(",").some(function(behaviour) {
    return isOneValidInput(behaviour, string);
  });
}

var string = 'BN123';
var behaviour = 'BN***,TA****';
console.log(isValidInput(behaviour, string));

(I stuck to ES5 there because you seemed to be doing so.)

2 Comments

Thank you! This seems the right way, using some(). Although, I'm getting an error 'isOneValidInput is not a function'. I'm using on react native, if that makes a difference.
@waze - You need to tweak it a bit, the above is standalone, but yours is in a class. Minor tweak.
0

Is this what you want?

var test = 'BN123';
var behaviour = 'BN***,TA****';

var behaviours = behaviour.split(',');
var result = behaviours.map(b => {
  if (b.length != test.length)
    return false;
  var pattern = b.split('*')[0];
  return pattern === test.substring(0,pattern.length);
}).find(r => r === true) > -1;


console.log(result)

Comments

0

You can use the new .includes() method to see if a string is contained within another. Note that this is case sensitive. I have included two dodgied up behaviours to check the string against.

var string = 'BN123';
var behaviour1 = 'BN123,TA1234';
var behaviour2 = 'BN120,TA1230';

function testStr(behaviour,string) {
 return behaviour.includes(string);
}


console.log(testStr(behaviour1,string)) // gives true
console.log(testStr(behaviour2,string)) // gives false

Comments

0
isValidInput(behaviour, string) {  
    var array = behaviour.split(",");
    var flag = 0;
    for(var i = 0;i< array.length;i++){
        var now = array[i];
        var _flag = 1;
        if (now.length == string.length) {
            for (var j = 0; j < now.length; j++) {
                if (now.charAt(j) == '*') {
                    continue;
                }

                if (now.charAt(j) != string.charAt(j)) {
                   _flag = 0;
                } 
            }
            flag |= _flag;
        }
    } 
    return flag;
}

Comments

0

Try modify your behaviour to RegExp:

function checkFn(testStr) {

    var behaviour = '(BN...)|(TA....)'

    var r = new RegExp('^(' + behaviour + ')$')

    return r.test(testStr)
}

checkFn('BN123') // true
checkFn('BN12') // false
checkFn('BN1234') // false
checkFn('TA1234') // true
checkFn('TA123') // false
checkFn('TA12345') // false

Or use this fn:

function checkFn(testStr) {

    var behaviour = 'BN***,TA****'

    behaviour = behaviour
        .split(',')
        .reduce((r, el) => {
            r.push('(' + el.replace(/\*/g, '.') + ')')
            return r
        }, [])
        .join('|')

    var r = new RegExp('^('+behaviour+')$')

    return r.test(testStr)
}

Comments

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.