[英]How can I compare words using regular expression but ignore certain ones?
I have made a search engine and I am comparing a search string against a list of words. 我做了一个搜索引擎,并且正在将搜索字符串与单词列表进行比较。 However I want to omit words like
"How,do,i"
. 但是我想省略诸如
"How,do,i"
。 So if the user search for "How do I find my IP?"
因此,如果用户搜索
"How do I find my IP?"
. 。 If I have 3 other items beginning with
How do I
it wouldn't really be able to return a good relevancy. 如果我还有3个其他的内容以
How do I
开头, How do I
它实际上将无法返回良好的相关性。
right now its setup (a-z0-9)+userinput+(a-z0-9)
Wanted to have a list of words to be omitted so they would not be matched. 现在,它的设置
(a-z0-9)+userinput+(a-z0-9)
希望有一个要省略的单词列表,以便它们不匹配。 I am using javascript only please assist. 我仅使用JavaScript,请协助。
Another updated example, now using \\b 另一个更新的示例,现在使用\\ b
<html>
<head>
<script type="text/javascript">
var unwanted = new RegExp(
"\\b("+ [
"(how|where|what) do (I|you)",
"some",
"other",
"words",
"or phrases",
"that",
"is",
"unwanted"
].join("|")+ ")(?=\\b)",
"ig" // Ignore case, global
);
function dosearch(e){
var search = e.search.value;
search = search.replace(unwanted,"");
alert(search);
return false;
}
</script>
</head>
<body>
<form method="post" action="" onsubmit="return dosearch(this)">
search: <input type="text" name="search">
</form>
</body>
</html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.