【问题标题】:match exact string in a sentence匹配句子中的精确字符串
【发布时间】:2011-03-21 14:54:09
【问题描述】:

如何精确匹配句子中的给定字符串。

例如,如果句子是 var sentence = "Google Wave 基本上是一个捕获通信的文档"

并且给定的字符串是 var inputString = "谷歌波"。我需要在上面的句子中检查 Google Wave 的确切存在并返回 true 或 false。

我试过了

if(sentence.match(inputString)==null){
            alert("No such word combination found.");
        return false;
        }

即使有人输入“Google W”,这也有效。我需要一种找到完全匹配的方法。请帮忙

【问题讨论】:

  • indexOf 也将返回一个子字符串,这不是他所要求的。请参阅下面的答案以获取潜在的解决方案

标签: javascript


【解决方案1】:

使用javascript的String.indexOf()

var str = "A Google wave is basically a document which captures a communication";
if (str.indexOf("Google Wave") !== -1){
  // found it
}

为了您不区分大小写的比较,并使其更容易:

// makes any string have the function ".contains([search term[, make it insensitive]])"
// usage:
//   var str = "Hello, world!";
//   str.contains("hello") // false, it's case sensitive
//   str.contains("hello",true) // true, the "True" parameter makes it ignore case
String.prototype.contains = function(needle, insensitive){
  insensitive = insensitive || false;
  return (!insensitive ?
    this.indexOf(needle) !== -1 :
    this.toLowerCase().indexOf(needle.toLowerCase()) !== -1
  );
}

糟糕,错误的文档参考。引用了 array.indexOf

【讨论】:

  • @Michael Jasper:OP 要求“确切存在”,所以在他的示例中,我猜他期望false
  • @MichaelJasper:怎么样? ;-)(虽然@pimvdb 是正确的,OP 想要“Google Wave”)
  • 如果句子是 "Two Google WAVES is basically a document which captures a communication" 我相信你的函数会返回 true, event 尽管 "Google Wave".toLowerCase() !== "Google Waves".toLowerCase()
  • @seebiscuit 查看以前的 cmets 关于完全匹配的信息。
【解决方案2】:

当使用Google W 进行搜索时,OP 希望返回 false。

我认为您应该为正则表达式使用单词边界。

http://www.regular-expressions.info/wordboundaries.html

示例:

inputString = "\\b" + inputString.replace(" ", "\\b \\b") + "\\b";
if(sentence.toLowerCase().match(inputString.toLowerCase())==null){
    alert("No such word combination found.");
}

【讨论】:

  • @dazzle 这是一个很好的问题,也许有一天我也需要答案。如果对您有帮助,请标记为已回答。谢谢。
【解决方案3】:

ContainsExactString2 只是我比必要的更深入,'===' 应该可以正常工作

<input id="execute" type="button" value="Execute" />

// Contains Exact String

$(function() {
    var s = "HeyBro how are you doing today";
    var a = "Hey";
    var b = "HeyBro";
    $('#execute').bind('click', function(undefined) {
        ContainsExactString(s, a);
        ContainsExactString(s, b);
    });
});

function ContainsExactString2(sentence, compare) {
    var words = sentence.split(" ");
    for (var i = 0; i < words.length; ++i) {
        var word = words[i];
        var pos = 0;
        for (var j = 0; j < word.length; ++j) {
            if (word[j] !== compare[pos]) {
                console.log("breaking");
                break;
            }
            if ((j + 1) >= word.length) {
                alert("Word was found!!!");
                return;
            }++pos;
        }
    }
    alert("Word was not found");
}

function ContainsExactString(sentence, compare) {
    var words = sentence.split(" ");
    for (var i = 0; i < words.length; ++i) {
        if(words[i] === compare) {
            alert("found " + compare);
            break;
        }
    }
    alert("Could not find the word");
    break;
}

【讨论】:

  • 我没有完全测试这个,我只是为了好玩,但这就是我让它工作的方式
猜你喜欢
  • 2016-03-08
  • 1970-01-01
  • 2021-10-18
  • 1970-01-01
  • 2022-11-26
相关资源
最近更新 更多