简体   繁体   English

检查字符串是否存在于另一个字符串中(不完全相等)

[英]Check if string exists in another string (not exactly equal)

I have this 2 strings: 我有这2个字符串:

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

I need to check if behaviour contains a string with the same format as test . 我需要检查behaviour包含具有与test相同格式的字符串。

On the behaviour , the BN and TA as to be equal, and the * means it can be any char. behaviourBNTA相等,并且*表示它可以是任何字符。 ( behaviour comes from an API, so I never know what it has, this is just a test.) behaviour来自API,所以我永远都不知道它有什么,这只是一个测试。)

In this case it should return true . 在这种情况下,它应该返回true Right now I'm only comparing is case behaviour as a single string, but I need to modify that: 现在,我只比较大小写behaviour为单个字符串,但我需要修改它:

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;
}

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). 我在您的实现中看到的唯一问题是,您不允许事实behaviour包含可能的用逗号分隔的字符串(或者至少对我来说是这样)。 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.) (我在那里坚持使用ES5,因为您似乎这样做了。)

You can use .some() of Array.prototype. 您可以使用Array.prototype的.some()

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"))); 

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) 

You can use the new .includes() method to see if a string is contained within another. 您可以使用新的.includes()方法查看字符串是否包含在另一个字符串中。 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 

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;
}

Try modify your behaviour to RegExp: 尝试将您的行为修改为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: 或使用此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)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM