简体   繁体   English

字符串匹配和不匹配的正则表达式问题

[英]Regex issue for string matching and NOT matching

Brand new to Javascript and read quite a bit but cant seem to get this right. 刚接触Javascript的人,虽然阅读了很多书,但似乎无法做到这一点。 I have dType linked to an onclick event within an HTML page. 我将dType链接到HTML页面内的onclick事件。 I cannot seem to get the syntax correct for the first else if statement where I would like the alert to occur if the user input is not a number and the input does not match 'true' or 'false'. 如果用户输入不是数字并且输入不匹配“ true”或“ false”,我似乎无法使第一个if语句的语法正确。 The second else if seems to be matching any string of equal length while I though this was making the array items case insensitive. 第二个if似乎与相等长度的任何字符串匹配,尽管这使数组项不区分大小写。 For the 4th is else, I was hoping to alert upon the user entering and value between -infinity and infinity. 对于第四点,我希望提醒用户输入-infinity和infinity之间的值。 the end goal is to provide some information about possible datatype despite the fact that, technically, anything entered into the prompt is a string. 最终目标是提供有关可能的数据类型的一些信息,尽管实际上从技术上讲,输入到提示中的任何内容都是字符串。 As much as I know there is alot of documentation on all of this I have been reading for hours and hoped asking directly would help me learn about what is going on here. 据我所知,我已经阅读了好几个小时的大量文档,希望直接问一下将有助于我了解这里的情况。 Thanks to anyone who took the time to read this despite its poor construction. 感谢任何花时间阅读这篇尽管结构较差的人。

 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') } } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

Your first else if contains syntax errors, mostly because you haven't put the regular expression in regex quotes ( /.../ ). 您的else if包含语法错误,主要是因为您没有将正则表达式放在正则表达式引号( /.../ )中。

The expression I think you're trying to use is also more complicated than it needs to be for what you've said you want, it can just be: 我认为您要使用的表达也比您想要表达的表达要复杂,它可以是:

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

...which means "match the start of input, followed by true or false , followed by the end of input". ...这意味着“匹配输入的开头,然后是truefalse ,然后是输入的结尾”。 (You were right you needed a group to ensure the alternation ( | ) only applies to the true / false and not the ^ and $ ; you used a capture group, which is okay; I used a non-capturing group instead above -- (?:...) -- as we don't use the captured text.) Then we negate the result (the ! ) because you don't want to match those. (您说对了,您需要一个组来确保交替( | )仅适用于true / false而不适用于^$ ;您使用了捕获组,没关系;我在上面使用了非捕获组- (?:...) -因为我们不使用捕获文本),然后我们否定的结果(。 ! !因为你不想匹配。

But typically, if you just want to know whether something matches, rather than String#match you'd use RegExp#test : 但是通常,如果您只是想知道某项是否匹配,而不是String#match可以使用RegExp#test

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

There are several other issues, however: 但是,还有其他几个问题:

  • You need to consistently have parens around the conditions in an if 您需要始终对if的条件保持一致

  • The second else if matches any character listed between the [ and ] , not a pair of choices 第二个else if匹配[]之间列出的任何字符,则不是一对选择

  • The conditions can be reordered to avoid duplicate checks 可以重新排列条件以避免重复检查

  • To check for infinity, you can use !isFinite 要检查无穷大,可以使用!isFinite

  • You need to either use or not use the i flag to ignore case consistently if you're going to check for something twice 如果要检查两次,则需要使用或不使用i标志来始终忽略大小写

Since we can't use alert and prompt in Stack Snippets, here's an example using an input field instead with a few of the above fixed: 由于我们无法在堆栈片段中使用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> 

There seems to be a couple of issues with the first else/if 第一个else / if似乎有几个问题

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

let's break it down. 让我们分解一下。 First you seem to have an extra ) after the isNaN call 首先,在isNaN调用之后,您似乎还有一个额外的)

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

That said, isNaN checks explicitly if a value is literally NaN , which will never be the case from a prompt() , so I would just remove that. 就是说, isNaN明确检查一个值是否为NaN ,这在prompt()是绝对不可能的,因此我将其删除。

Secondly, you need to wrap your regex in / like so: /^(?!'true' || 'false').)*$/ , as pointed out by TJ it can also be made a bit simpler. 其次,您需要将正则表达式包装在/如下所示: /^(?!'true' || 'false').)*$/ ,正如TJ指出的那样,它也可以变得更简单。

...for the first else if statement where I would like the alert to occur if the user input is not a number and the input does not match 'true' or 'false' ...对于第一个else if语句,如果用户输入的内容不是数字并且输入的内容与'true'或'false'不匹配,我希望在其中发出警报

You could rearrange the order to simply match true/false, then check for digits etc, to make each if statement simpler; 您可以重新排列顺序以简单地匹配true / false,然后检查数字等,以使每个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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM