Javascript 如何检查字符串是否包含特定的子字符串

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

How to check if a string contains a specific substring

javascriptjquery

提问by Andrew Ng

I'm working on a simple if/else jquery statement and I ran into some trouble with the variable. What I need it to do is to check if the var is true. In my case I want it to check if the has 'dont push' in it. If it's true, the html must change to 'lol'. If not, it will give a simple alert. Can anybody give me some guidance here?

我正在处理一个简单的 if/else jquery 语句,但在使用该变量时遇到了一些麻烦。我需要它做的是检查 var 是否为真。在我的情况下,我希望它检查是否有“不要推”。如果是这样,html 必须更改为“lol”。如果没有,它会给出一个简单的警报。有人可以在这里给我一些指导吗?

<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="user-scalable=no, width=device-width" />
<title>Untitled Document</title>
<link href="mobile.css" rel="stylesheet" type="text/css" media="only screen and (max-width: 480px)" />
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
    $('#nietklikken').click(function() {
        var n = $(this).html('dont push');
        if (n == "$(this).html('dont push')"){
            $(this).html('lol')     
        } else {
            alert('lolz');
        }
    });
});
</script>
</head>

<body>
<button type="button" id="nietklikken">dont push</button>

</body>
</html>

回答by mmhan

$(this).html()will getthe innerhtml value and $(this).html(arg)will setthe innerhtml value. The correct usage will be below.

$(this).html()getinnerHTML值和$(this).html(arg)setinnerHTML值。正确用法如下。

$('#nietklikken').click(function() {
    if ($(this).html().indexOf('dont push') > -1){
        $(this).html('lol')     
    } else {
        alert('lolz');
    }
});

You should read up more in jquery docs.

您应该在jquery docs 中阅读更多内容。

Update: It will now check if the innerhtml contains 'dont push'.

更新:它现在将检查innerhtml 是否包含'dont push'。

回答by ipr101

You could use the 'indexOf' operator that should tell you if a string contains another string. Try -

您可以使用“ indexOf”运算符来告诉您一个字符串是否包含另一个字符串。尝试 -

if ($(this).html().indexOf('dont push') != -1){
            $(this).html('lol')     
        } else {
            alert('lolz');
        }

回答by Ali

In general you could use a function like this

一般来说,你可以使用这样的函数

function HasSubstring(string,substring){
    if(string.indexOf(substring)>-1)
      return true;
      return false;
}

回答by Greg B

You can get the HTML content of an element by calling the html()method with no arguments like so:

您可以通过调用html()不带参数的方法来获取元素的 HTML 内容,如下所示:

var innerHtml = $(someElement).html();

You can then check for the presence of a string by using indexOflike so:

然后,您可以使用indexOf如下方式检查字符串是否存在:

var position = "Find the string".indexOf("the");
// position = 5

If the given string isn't present, indexOfwill return -1.

如果给定的字符串不存在,indexOf将返回 -1。

var position = "Find the string".indexOf("notFound");
// position = -1

You can then use this in an ifstatement like so

然后你可以在这样的if语句中使用它

if($(someElement).html().indexOf("dont push") >-1)
{
    // Required action here
}

回答by T.Todua

A working javascript code to check if string contains the specific word/substring:

用于检查字符串是否包含特定单词/子字符串的有效 javascript 代码:

var inputString = "this is my sentence";
var findme = "my";

if ( inputString.indexOf(findme) > -1 ) {
  out.print( "found it" );
} else {
  out.print( "not found" );
}