简体   繁体   English

使用Javascript检查字符串是否包含HTML中的单词或子字符串

[英]Check if String Contains a word or Substring in HTML using Javascript

Im trying to get the value of my admin textarea and check if the textarea of user have the substring or word that matches the admin value, when the user input 1 word and matches the admin it would say correct but when the user input a sentence with the correct word in admin it would say error instead of correct can you help pls我试图获取我的 admin textarea 的值并检查用户的 textarea 是否具有与 admin 值匹配的子字符串或单词,当用户输入 1 个单词并匹配 admin 时,它会说正确,但是当用户输入一个句子时管理中的正确词会说错误而不是正确的,你能帮忙吗

 var a = document.getElementById('admin'); var b = document.getElementById('user'); function sample() { if (a.value.includes(b.value)) { document.getElementById('output').innerHTML = "USER INPUT WITH KEYWORD"; } else { document.getElementById('output').innerHTML = "USER INPUT WITHOUT KEYWORD"; } }
 <textarea id="admin" placeholder="admin"></textarea> <textarea id="user" placeholder="user"></textarea> <textarea id="output"></textarea> <button style="height: 50px; width: 100px;" onclick="sample()"> </buttom>

As @Chris G said,正如@Chris G 所说,

Your code is checking if the user value is inside the admin value, not the other way around.您的代码正在检查用户值是否在 admin 值内,而不是相反。 Instead of a and b , use descriptive variable names to prevent basic mistakes like this.而不是ab ,使用描述性变量名称来防止这样的基本错误。

In your code, you are checking if the admin value has the substring or word that matches the user value, but in your question, you wanted it the other way around.在您的代码中,您正在检查 admin 值是否具有与用户值匹配的子字符串或单词,但在您的问题中,您希望它反过来。 One simple way to prevent this mistake is by double checking your variables, or by putting in more descriptive names.防止这种错误的一种简单方法是仔细检查变量,或者输入更具描述性的名称。 For example, instead of using a and b as your variable names, use admin and user .例如,不要使用ab作为变量名,而是使用adminuser
Corrected Code:更正的代码:

var a = document.getElementById('admin');
var b = document.getElementById('user');

function sample() {
  if (b.value.includes(a.value)) {
    document.getElementById('output').innerHTML = "USER INPUT WITH KEYWORD";
  } else {
    document.getElementById('output').innerHTML = "USER INPUT WITHOUT KEYWORD";
  }
}

Corrected Code with better practices:使用更好的实践更正代码:

var admin = document.getElementById('admin');
var user = document.getElementById('user');

function sample() {
  if (user.value.includes(admin.value)) {
    document.getElementById('output').innerHTML = "USER INPUT WITH KEYWORD";
  } else {
    document.getElementById('output').innerHTML = "USER INPUT WITHOUT KEYWORD";
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 检查字符串是否包含单词(不是子字符串) - Check if string contains word (not substring) 检查字符串是否包含JavaScript中的子字符串 - Check string contains substring in javascript 不使用任何标准JavaScript方法检查字符串是否包含子字符串? - Check if string contains substring without using any standard JavaScript methods? 检查字符串是否包含子字符串而不使用 indexOf - Javascript - Check if string contains substring without using indexOf - Javascript 如何检查字符串是否包含子字符串并在javascript中为其着色? - how to check if a string contains substring and color it in javascript? 如何在JavaScript中检查字符串是否包含子字符串 - How to check string contains substring in javascript 如何检查一个字符串在JavaScript中是否包含一个substring? - How to check whether a string contains a substring in JavaScript? 如果字符串包含单词,则使用Javascript过滤HTML ul li - Using Javascript to Filter a HTML ul li if string contains word 如何使用Javascript RegExp匹配和替换包含子字符串的整个单词 - How to match and replace entire word that contains substring using Javascript RegExp 检查字符串数组是否包含javascript中给定字符串的子字符串? - Check if an array of strings contains a substring of a given string in javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM