首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如果在文本上找到了任何单词,我该如何判断?

如果在文本上找到了任何单词,我该如何判断?
EN

Stack Overflow用户
提问于 2019-10-17 01:09:47
回答 3查看 59关注 0票数 0

我尝试检查是否包含文本console.log(1)console.log(2)上的任何单词来表示false,但我得到了1,2,1,2,因为它是匹配的,有些失败,但我想如果只包含给定单词中的一个单词,则为ok,但如果不包含false,但我得到true,false

代码语言:javascript
复制
var keywords = ['aslr', 'ida pro', 'gdb', 'windbg', 'immunity debugger', 'boofuzz', 'peach fuzzer', 'winafl', 'python', 'assembly', 'penetration testing', 'exploits', 'metasploit', 'metasploit framework', 'ethical hacker', 'pentest', 'computer security', 'hacking', 'oscp', 'osce', 'osee', 'penetration testing', 'offensive security', 'mitre att&ck', 'vulnerability research', 'vulnerability researcher', 'fuzzing', 'clang', 'llvm', 'address sanitizer', 'afl', 'fuzzers', 'penetration tester']

var data = "a successful cybersecurity consultancy are seeking an experienced penetration tester to join their melbourne practice on a permanent basis. work across a wide portfolio of clients and help them in identifying security vulnerabilities by conducting web application, network security, and wireless penetration testing. key responsibilities/duties: work with a diverse range of customers to identify and solve security problems, both in-person and remotely undertake application, network, and wireless penetration testing and vulnerability assessments prepare high-quality reports detailing security issues, making recommendations and identifying solutions perform social engineering and physical security assessments and/or undertake secure code reviews, where appropriate key requirements: demonstrated experience in penetration testing penetration testing certifications such as oscp, osce, ceh, sans, crest crt or cct a proven passion for cybersecurity with regular attendance at security events and/or memberships to the likes of owasp understanding of information security principles and security technologies 2 + years' work experience in security what the company can offer you: ongoing one-on-one training and development a budget for training courses/certifications flexible working arrangements and the option to work-from-home the opportunity to work in a collaborative environment with experienced security professionals if this position sounds of interest, please click 'apply' or email your cv directly to charlotte@preactarecruitment.com."

for (var i = 0; i < keywords.length; i += 1) {
  if (data.indexOf(keywords[i])) {
    console.log(1)
  }
  console.log(2)
}

EN

回答 3

Stack Overflow用户

发布于 2019-10-17 01:12:21

使用通用变量来计算匹配,然后检查变量,而不是记录在循环中。

例如:

代码语言:javascript
复制
let matches = 0;

for (var i = 0; i < keywords.length; i += 1) {
    // Note that indexOf returns -1 on failure, not false or zero
    if (data.indexOf(keywords[i]) !== -1) {
        matches++;
    }
}

if(matches) {
    console.log(1);
}
else {
    console.log(2);
}
票数 0
EN

Stack Overflow用户

发布于 2019-10-17 01:21:29

您可以结合使用someincludes来检查keywords中的单词是否在data中,然后记录所需的输出:

代码语言:javascript
复制
var keywords = ['aslr', 'ida pro', 'gdb', 'windbg', 'immunity debugger', 'boofuzz', 'peach fuzzer', 'winafl', 'python', 'assembly', 'penetration testing', 'exploits', 'metasploit', 'metasploit framework', 'ethical hacker', 'pentest', 'computer security', 'hacking', 'oscp', 'osce', 'osee', 'penetration testing', 'offensive security', 'mitre att&ck', 'vulnerability research', 'vulnerability researcher', 'fuzzing', 'clang', 'llvm', 'address sanitizer', 'afl', 'fuzzers', 'penetration tester'];
var data = "a successful cybersecurity consultancy are seeking an experienced penetration tester to join their melbourne practice on a permanent basis. work across a wide portfolio of clients and help them in identifying security vulnerabilities by conducting web application, network security, and wireless penetration testing. key responsibilities/duties: work with a diverse range of customers to identify and solve security problems, both in-person and remotely undertake application, network, and wireless penetration testing and vulnerability assessments prepare high-quality reports detailing security issues, making recommendations and identifying solutions perform social engineering and physical security assessments and/or undertake secure code reviews, where appropriate key requirements: demonstrated experience in penetration testing penetration testing certifications such as oscp, osce, ceh, sans, crest crt or cct a proven passion for cybersecurity with regular attendance at security events and/or memberships to the likes of owasp understanding of information security principles and security technologies 2 + years' work experience in security what the company can offer you: ongoing one-on-one training and development a budget for training courses/certifications flexible working arrangements and the option to work-from-home the opportunity to work in a collaborative environment with experienced security professionals if this position sounds of interest, please click 'apply' or email your cv directly to charlotte@preactarecruitment.com.";

if (keywords.some(word => data.includes(word))) console.log(1);
else console.log(2);

票数 0
EN

Stack Overflow用户

发布于 2019-10-17 01:25:17

您可以使用一个变量来检查找到的关键字的数量,因为您只想要一个,如果count的值超出了1,则中断循环并相应地显示值

代码语言:javascript
复制
var keywords = ['aslr', 'ida pro', 'gdb', 'windbg', 'immunity debugger', 'boofuzz', 'peach fuzzer', 'winafl', 'python', 'assembly', 'penetration testing', 'exploits', 'metasploit', 'metasploit framework', 'ethical hacker', 'pentest', 'computer security', 'hacking', 'oscp', 'osce', 'osee', 'penetration testing', 'offensive security', 'mitre att&ck', 'vulnerability research', 'vulnerability researcher', 'fuzzing', 'clang', 'llvm', 'address sanitizer', 'afl', 'fuzzers','penetration tester']
var data = "a successful cybersecurity consultancy are seeking an experienced penetration tester to join their melbourne practice on a permanent basis. work across a wide portfolio of clients and help them in identifying security vulnerabilities by conducting web application, network security, and wireless penetration testing. key responsibilities/duties: work with a diverse range of customers to identify and solve security problems, both in-person and remotely undertake application, network, and wireless penetration testing and vulnerability assessments prepare high-quality reports detailing security issues, making recommendations and identifying solutions perform social engineering and physical security assessments and/or undertake secure code reviews, where appropriate key requirements: demonstrated experience in penetration testing penetration testing certifications such as oscp, osce, ceh, sans, crest crt or cct a proven passion for cybersecurity with regular attendance at security events and/or memberships to the likes of owasp understanding of information security principles and security technologies 2 + years' work experience in security what the company can offer you: ongoing one-on-one training and development a budget for training courses/certifications flexible working arrangements and the option to work-from-home the opportunity to work in a collaborative environment with experienced security professionals if this position sounds of interest, please click 'apply' or email your cv directly to charlotte@preactarecruitment.com."

let matchCount = 0
for (var i = 0; i < keywords.length; i += 1) {
  if (data.includes(keywords[i])) {
    matchCount++
  }
  if (matchCount > 1) break
}

console.log(matchCount === 1 ? 1 : 2)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58418469

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档