【问题标题】:Regex issue for string matching and NOT matching字符串匹配和不匹配的正则表达式问题
【发布时间】:2016-02-14 20:27:33
【问题描述】:

JavaScript 新手,阅读了很多,但似乎无法正确理解这一点。我将 dType 链接到 HTML 页面中的 onclick 事件。如果用户输入不是数字并且输入与“true”或“false”不匹配,我似乎无法获得第一个 else if 语句的语法正确,我希望在该语句中出现警报。第二个 else if 似乎匹配任何相等长度的字符串,而我虽然这使数组项不区分大小写。对于第 4 点,我希望在用户输入和值介于 -infinity 和 infinity 之间时发出警报。最终目标是提供有关可能数据类型的一些信息,尽管从技术上讲,输入到提示中的任何内容都是字符串。据我所知,我已经阅读了几个小时的所有这些文档,并希望直接询问可以帮助我了解这里发生了什么。感谢任何花时间阅读本文的人,尽管它的结构很差。

function dType() {
    var inputP= prompt("Type something Nice and Ill tell you something useful:");

    if (inputP === "") {
 	    alert('At least give me something...');
    } else if (isNaN(inputP)) && inputP.match(^(?!'true' || 'false').)*$){
 	    alert('This is a String');
    } else if inputP.match(/['true','false']/i){
	    alert('This might be a boolean');
    } else if inputP.match(/\d+/g){
	    alert('This has some numbers in it')
    } else if inputP = (inputP > MATH.min('') && inputP < MATH.max('')){
        alert('this is a number')
    }
}
&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"&gt;&lt;/script&gt;

【问题讨论】:

  • 使用 !/true/.test(inputP) &amp;&amp; !/false/.test(inputP) 代替 inputP.match(^(?!'true' || 'false').)*$)。同样,尝试将inputP.match(/\d+/g) 替换为/\d+/.test(inputP)

标签: javascript jquery regex


【解决方案1】:

您的第一个 else if 包含语法错误,主要是因为您没有将正则表达式放在正则表达式引号中 (/.../)。

我认为你试图使用的表达方式也比你所说的想要的更复杂,它可以是:

} else if (isNaN(inputP)) && !inputP.match(/^(?:true|false)$/)) {

...表示“匹配输入的开头,然后是truefalse,然后是输入的结尾”。 (你说得对,你需要一个组来确保交替 (|) 仅适用于 true/false 而不是 ^$;你使用了一个捕获组,这没关系;我在上面使用了一个非捕获组——(?:...)——因为我们不使用捕获的文本。)然后我们否定结果(!)因为你想要匹配那些。

但通常,如果您只是想知道某些内容是否匹配,而不是String#match,您会使用RegExp#test

} else if (isNaN(inputP)) && !/^(?:true|false)$/.test(inputP)) {

不过还有其他几个问题:

  • 您需要始终在 if 中的条件周围加上括号

  • 第二个else if 匹配[] 之间列出的任何字符,而不是一对选择

  • 条件可以重新排序以避免重复检查

  • 要检查无穷大,可以使用!isFinite

  • 如果您要检查两次,则需要使用或不使用 i 标志来忽略大小写一致

由于我们不能在 Stack Snippets 中使用 alertprompt,下面是一个使用输入字段的示例,而不是上面的一些修复:

$("input[type=button]").on("click", function() {
  dType($("input[type=text]").val());
});

function dType(inputP) {
  if (inputP === "") {
    show('At least give me something...');
  } else if (/^(?:true|false)$/i.test(inputP)) {
    show('This might be a boolean');
  } else if (isNaN(inputP)) {
    show('This is a String');
  } else if (!isFinite(inputP)) {
    show('this is infinity')
  } else if (isFinite(inputP)) {
    show('this is a number')
  } else if (/\d+/.test(inputP)) {
    show('This has some numbers in it')
  } else {
    show("dunno");
  }
}

function show(msg) {
  $("<p>").text(msg).appendTo(document.body);
}
<input type="text" id="prompt">
<input type="button" value="Go">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

【讨论】:

  • 你不需要“不捕获”来使用备用字符串检查regex101.com/r/qM6oG2/1
  • @Blag:不,你没有。我总是这样做,因为它对正则表达式引擎的工作较少。请注意,如果您使用RegExp#test,我希望引擎知道它不必保留捕获。
  • so the | gets applied correctly only to true and false and not to the ^ and $.这句话不是真的,我就是这个意思;)
  • @Blag:啊。您的示例仍然有一个组,所以我通过您只是在谈论类型。是的,必须使用组。如果没有某种类型的组,您将匹配 truealsdkjflasaflksjdlfkajsdfalseregex101.com/r/gI9tB8/1
  • 这不是我的意思:您的句子似乎告诉“| 仅在非捕获组中工作”
【解决方案2】:

第一个 else/if 似乎有几个问题

} else if (isNaN(inputP)) && inputP.match(^(?!'true' || 'false').)*$){

让我们分解一下。 首先,在isNaN 调用之后,您似乎多了一个)

isNan(inputP) // <-- ends the isNaN call
if (isNan(inputP)) // <-- ends the if condition
if (isNan(inputP) && ...) // what you probably wanted

也就是说,isNaN 会明确检查一个值是否是字面上的 NaN,而 prompt() 绝不会出现这种情况,所以我将删除它。

其次,您需要将您的正则表达式包装在 / 中,如下所示:/^(?!'true' || 'false').)*$/,正如 T.J.也可以简单一点。

...对于第一个 else if 语句,如果用户输入不是数字并且输入与“真”或“假”不匹配,我希望在该语句中发生警报

您可以重新排列顺序以简单地匹配真/假,然后检查数字等,以使每个 if 语句更简单;

if (!inputP) { // an empty string is "falsy"
    // at least give me something
} else if (/^(true|false)$/.test(inputP)) {
    // inputP is "true" or "false"
} else if (/^\d+$/.test(inputP)) {
    // inputP contains only numbers
} else if (/\d+/.test(inputP)) {
    // inputP contains some numbers
} else {
    // inputP is a string
}

【讨论】:

  • 感谢您的建议,非常有帮助。我喜欢重新排列的方法!我会尝试实现这一点。感谢您解决我对 isNaN 的误解和语法问题
猜你喜欢
  • 2016-07-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-13
  • 2020-07-12
  • 2011-11-08
  • 2012-06-05
相关资源
最近更新 更多