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"
. So if the user search for "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.
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. I am using javascri开发者_StackOverflow中文版pt only please assist.
Another updated example, now using \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>
精彩评论