javascript 匹配句子中的精确字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5379365/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 16:59:54  来源:igfitidea点击:

match exact string in a sentence

javascript

提问by dazzle

How to exactly match a given string in a sentence.

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

For example if the sentence is var sentence = "A Google wave is basically a document which captures a communication"

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

and the given string is var inputString= "Google Wave". I need to check the exact presence of Google Wave in the above sentence & return true or false.

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

I tried

我试过

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

This works even if someone enters "Google W". I need a way to find the exact match. Please help

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

回答by Thomas Li

OP wants to return false when do search with Google W.

OP 想在使用Google W.

I think you should use word boundary for regular expression.

我认为您应该将字边界用于正则表达式。

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

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

Sample:

样本:

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

回答by Brad Christie

Using javascript's String.indexOf().

使用 javascript 的String.indexOf().

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

For your case-insensitive comparison, and to make it easier:

对于不区分大小写的比较,并使其更容易:

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

Oop, wrong doc reference. Was referencing array.indexOf

哎呀,错误的文档参考。正在引用 array.indexOf

回答by dazzle

ContainsExactString2 was just me going more in-depth than necessary, '===' should work just fine

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