Javascript jQuery 在变量中搜索文本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4581625/
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-08-23 13:04:31  来源:igfitidea点击:

jQuery Search for Text in a Variable?

javascriptjquery

提问by thatryan

I have a variable that contains some text, some html, basically can be a string. I need to search the variable for a specific string to process that variable differently if it is contained. Here is a snippet of what I am trying to do, does not work obviously :)

我有一个包含一些文本、一些 html 的变量,基本上可以是一个字符串。我需要在变量中搜索特定字符串,以便在包含该变量时以不同方式处理该变量。这是我正在尝试做的一个片段,显然不起作用:)

$.each(data.results,
   function(i, results) {
   var text = this.text                   
   var pattern = new RegExp("^[SEARCHTERM]$");
    if(pattern.test( text ) )
    alert(text);  //was hoping this would alert the SEARCHTERM if found...

回答by user113716

You could use .indexOf()instead to perform the search.

您可以.indexOf()改为使用来执行搜索。

If the string is not found, it returns -1. If it is found, it returns the first zero-based indexwhere it was located.

如果未找到该字符串,则返回-1。如果找到,则返回其所在位置的第一个从零开始的索引

var text = this.text;
var term = "SEARCHTERM";

if( text.indexOf( term ) != -1 )
    alert(term);

If you were hoping for an exact match, which your use of ^and $seems to imply, you could just do an ===comparison.

如果您希望精确匹配,您的使用^$似乎暗示了这一点,您可以进行===比较。

var text = this.text;
var term = "SEARCHTERM";

if( text === term )
    alert(term);


EDIT:Based on your comment, you want an exact match, but ===isn't working, while indexOf()is. This is sometimes the case if there's some whitespace that needs to be trimmed.

编辑:根据您的评论,您想要完全匹配,但===不起作用,indexOf()而是。如果有一些空白需要修剪,有时就会出现这种情况。

Try trimming the whitespace using jQuery's jQuery.trim()method.

尝试使用jQuery 的jQuery.trim()方法修剪空格。

var text = $.trim( this.text );
var term = "SEARCHTERM";

if( text === term )
    alert(term);

If this doesn't work, I'd recommend logging this.textto the console to see if it is the value you expect.

如果这不起作用,我建议登录this.text到控制台以查看它是否是您期望的值。

回答by PleaseStand

If you are using a regular expression to search for a substring, you can find the match like this:

如果您使用正则表达式来搜索子字符串,您可以像这样找到匹配项:

var match = text.match(pattern);
if(match) {
    alert(match[0]);
}

See the documentation for .match()for more information.

有关.match()更多信息,请参阅文档。

Note that if you are trying to search for the string "[SEARCHTERM]"(a search term containing brackets), your search method will not work because the brackets have a special meaning within regular expressions. Instead, use .indexOf():

请注意,如果您尝试搜索字符串"[SEARCHTERM]"(包含括号的搜索词),您的搜索方法将不起作用,因为括号在正则表达式中具有特殊含义。相反,使用.indexOf()

var index = text.indexOf("[SEARCHTERM]");
if(index != -1) {
    alert("Search term found");
}

However, if you are just trying to find an exact match (nothing else but the search term is in the string you are checking), you should just compare the strings:

但是,如果您只是想找到一个完全匹配的(除了搜索词在您检查的字符串中),您应该只比较字符串:

if(text == "[SEARCHTERM]") {
    alert("Search term found");
}

回答by Lightness Races in Orbit

What does "does not work" mean here?

这里的“不起作用”是什么意思?

Be aware that you should use braces ({}) with ifstatements. Some browsers will allow you to miss them out but you should include them to be correct and to support everything.

请注意,您应该{}if语句中使用大括号 ( ) 。有些浏览器会让您错过它们,但您应该将它们包含在内以确保正确并支持所有内容。