正则表达式中的多个匹配项

时间:2011-03-26 03:11:14

标签: javascript

您好我正在尝试使用以下javascript代码。当我键入包含所有三个名称的行时 发烧,夜间吃饭和夜尿 - 我想要一个提醒窗口来提醒我,这三个组合代表一个危险的标志。我究竟做错了什么?非常感谢!

<html>
<script type="text/javascript">
function show_prompt() {
    var q = prompt("Please enter your name", "");

    var myRegExp1 = ^ ( ? = . * fever)( ? = . * night sweats)( ? = . * nocturia);
    var myRegExp2 = /fever,weight-loss,nocturia|weight-loss,fever,nocturia|nocturia,weight-loss,fever/
    var string1 = q;
    var string2 = q;
    var matchPos1 = string1.search(myRegExp1);
    var matchPos2 = string2.search(myRegExp2);
    if (matchPos1 != -1) {
        alert("bingo" + matchPos1);

    }
    if (matchPos2 != -1) {
        alert("symptoms" + matchPos2);
    }

}
</script>
<body>

<input type="button" onclick="show_prompt()" value="Show prompt box" />

</body>
</html>

2 个答案:

答案 0 :(得分:0)

首先,您遇到语法错误:

var myRegExp1 = ^ ( ? = . * fever)( ? = . * night sweats)( ? = . * nocturia);

需要

var myRegExp1 = /^ ( ? = . * fever)( ? = . * night sweats)( ? = . * nocturia)/;

答案 1 :(得分:0)

如果我理解正确,我认为这就是你想要的。 Please test it here.

function show_prompt() {
    var q = prompt("Please enter your name", "");

    var myRegExp = /(fever|weight-loss|nocturia),(fever|weight-loss|nocturia),(fever|weight-loss|nocturia)/;
    var string = q;
    var matchPos = string.search(myRegExp);
    if (matchPos != -1) {
        alert("symptoms" + matchPos);
    }
}
相关问题