最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Use javascript to return true if a string contains an exact match - Stack Overflow

programmeradmin2浏览0评论

I've been using the .indexOf('') > -1 in order to check whether there's a match in a string. The problem that I'm having is that when I'm performing the match on multiple strings, I get a match on the string for both EIFT and EI (since EIFT contains EI), and so the function returns true for both sentences. What I need is a way for this to only return true for function eIft if the string is "EIFT", but not for EI.

My current code is as follows, and I've been trying to think of ways around this but haven't had any success yet.

function eI(mystring){
    return mystring.indexOf("EI") > -1
}

function eIft(mystring){
    return mystring.indexOf("EIFT") > -1
}

Thanks!

I've been using the .indexOf('') > -1 in order to check whether there's a match in a string. The problem that I'm having is that when I'm performing the match on multiple strings, I get a match on the string for both EIFT and EI (since EIFT contains EI), and so the function returns true for both sentences. What I need is a way for this to only return true for function eIft if the string is "EIFT", but not for EI.

My current code is as follows, and I've been trying to think of ways around this but haven't had any success yet.

function eI(mystring){
    return mystring.indexOf("EI") > -1
}

function eIft(mystring){
    return mystring.indexOf("EIFT") > -1
}

Thanks!

Share Improve this question edited Sep 10, 2014 at 21:55 AgileDan 3611 gold badge2 silver badges21 bronze badges asked Sep 10, 2014 at 21:18 maudulusmaudulus 11.1k11 gold badges85 silver badges121 bronze badges 5
  • Can you clarify what is wrong with the function eIft? It will return false if myString contains EI but not EIFT. – cybersam Commented Sep 10, 2014 at 21:26
  • I don't understand. If it contains EIFT then it also contains EI, so why shouldn't it match? Are you saying that you want to prefer the longer over the shorter? If so, then search for the longer first. – cookie monster Commented Sep 10, 2014 at 21:27
  • Is there a reason you don't respond to requests for clarification? – cookie monster Commented Sep 10, 2014 at 21:44
  • @cookiemonster @cybersam, the functions work, but the problem lies in that the function eI will return true for string "blah blah eift", when I only want function eIft to return true for "blah blah eift". Hope that clears up your questions :) – maudulus Commented Sep 11, 2014 at 13:39
  • @maudulus: No, not really. It's an unreasonable expectation for the eI function to not return true when it's designed to return true. And it's even less clear now that the answer you accepted below would return false for both functions. Your problem's description is vague. What is the XY Problem – cookie monster Commented Sep 11, 2014 at 18:13
Add a ment  | 

2 Answers 2

Reset to default 2

You can use ===; that will do an exact match of strings. Use indexOf only if you're checking whether the string contains another string.

function eI (mystring) {
    return mystring === "EI";
}

function eIFt(mystring) {
    return mystring === "EIFT";
}

If you are checking inside a string for you values (e.g. heleilo), then you need to confirm your positive results for the 'EI' check:

function eI(mystrng) {
    return mystring.indexOf("EI") != -1 && !eIFt(mystring);
}

This would only work provided they don't both exist in different occurences (e.g. heleileifto). In this case, you have to check the immediate following characters:

function eI(mystring) {
    var pos = mystring.indexOf("EI");

    if (pos != -1) { // found
        var char1 = mystring[pos + 2];
        var char2 = mystring[pos + 3];

        return char1 !== 'F' && char2 !== 'T';
    }
}

OR

function eI(mystring) {
    var pos = mystring.indexOf("EI");

    if (pos != -1) { // found
        return pos != eIFt(mystring); // they won't have the same index
    }
}
发布评论

评论列表(0)

  1. 暂无评论