简体   繁体   English

如何使用正则表达式比较单词却忽略某些单词?

[英]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.

相关问题 如何忽略正则表达式中的某些字符? - How to ignore certain characters in regular expression? 如何使用JavaScript正则表达式匹配包含两个相同单词的字符串中的单词? - How can i match a word in a string which has two same words before it using JavaScript regular expression? 如何使用正则表达式使单词脱离括号 - How do I get the words out of parenthesis using regular expression 是否有一个正则表达式会忽略被引号包围的单词 - Is there a regular expression that will ignore words that are surronded by quotes 以特定字符串开头的单词的正则表达式(javascript) - regular expression for words beginning with a certain string (javascript) 使用 TableHTMLExport,我如何告诉它忽略某些 div 和跨度? - using TableHTMLExport, how can i tell it to ignore certain divs and spans? 如何使用正则表达式使用Javascript替换字符串中特定单词之外的所有内容 - How can I use a Regular Expression to replace everything except specific words in a string with Javascript 如何使用正则表达式按空格拆分字符串并忽略前导和尾随空格到单词数组中? - How do I split a string by whitespace and ignoring leading and trailing whitespace into an array of words using a regular expression? 如何使用正则表达式检查单词是否带有括号 - How can I check if a word has parentheses using regular expression 如何使用正则表达式验证javascript中的URL - how can i validate a url in javascript using regular expression
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM