除了给定字符串之外,匹配任何内容的JavaScript模式是什么?

时间:2022-12-05 21:25:36

I need to make sure a given string is not "Select a value". That's it, anything else should match the pattern, except this exact string.

我需要确保给定的字符串不是“选择一个值”。就是这样,除了这个确切的字符串之外,其他任何东西都应该匹配模式。

I've been looking around and trying many combinations on http://www.regular-expressions.info/javascriptexample.html but nothing seems to do the trick.

我一直在环顾四周,在http://www.regular-expressions.info/javascriptexample.html上尝试了很多组合,但似乎没有什么可以做到的。

I can't negate the test, the pattern needs to do it all since I'll feed this to a form validation framework. If the select contains this text, I'll return an error, etc. So the pattern must match anything else, except this exact string,

我不能否定测试,模式需要做到这一切,因为我将它提供给表单验证框架。如果select包含这个文本,我将返回一个错误,等等。所以模式必须匹配其他任何东西,除了这个确切的字符串,

I tried lots of things,

我尝试了很多东西,

(?!Select an account)
!(Select an account)
!(^(Select an account)$)

etc... clearly I don't understand how some of these mechanisms work. I get the "starts with" and 'ends with", but I don't seem to find a simple negation operator.

等等......显然我不明白其中一些机制是如何运作的。我得到“开头”和“结束”,但我似乎没有找到一个简单的否定运算符。

For some reason everywhere I look for regex explanations I don't get this simple use case, maybe it's not common.

出于某种原因我到处寻找正则表达式的解释我没有得到这个简单的用例,也许它并不常见。

How can I accomplish this?

我怎么能做到这一点?

Thank you!

4 个解决方案

#1


10  

I believe this was asking something similar:

我相信这是在问类似的东西:

Regular Expressions and negating a whole character group

正则表达式和否定整个字符组

In your case you could use something like

在你的情况下你可以使用类似的东西

    ^(?!Select a value).*$

Which would match everything that does NOT start with "Select a value."

这将匹配所有不以“选择值”开头的内容。

#2


2  

Instead of thinking about a negative regular expression, make a positive one and negate the condition:

而不是考虑消极的正则表达式,做出积极的正则表达式并否定条件:

if (! /^Select an account$/.test(mystring)) {
  // String is not "Select an account"
}

But you don't need regex for this anyway:

但是你无论如何都不需要正则表达式:

if (mystring != 'Select an account') {
  // String is not "Select an account"
}

Or if you want "contains":

或者如果你想要“包含”:

if (mystring.indexOf('Select an account') == -1) {
  // String does not contain "Select an account"
}

#3


0  

I may not be understanding this correctly. Wouldn't this work?

我可能没有正确理解这一点。这不行吗?

if (string != "Select a value") {
    //code here
}

I don't know why you would need regular expressions to do that, which is why I'm afraid I might not be understanding what your question is.

我不知道为什么你需要正则表达式才能做到这一点,这就是为什么我可能不理解你的问题是什么。

#4


0  

An additional solution, if you’re dealing with regex in HTML5 form validation and the pattern attribute of your input is more complex than one exact string, is as follows (javascript):

另一个解决方案,如果您在HTML5表单验证中处理正则表达式并且输入的模式属性比一个完全字符串更复杂,则如下(javascript):

function findAllNotPattern(inputField) {
    var allBut1 = inputField.getAttribute("pattern")
    var allBut2 = allBut1.slice(0, 1) + "^" + allBut1.slice(1)
    var allButRe = new RegExp(allBut2, "g")
    return allButRe
}

if you want to run it in pure javascript, you'd use:

如果你想在纯JavaScript中运行它,你会使用:

findAllNotPattern(document.getElementById("inputFieldId"))

#1


10  

I believe this was asking something similar:

我相信这是在问类似的东西:

Regular Expressions and negating a whole character group

正则表达式和否定整个字符组

In your case you could use something like

在你的情况下你可以使用类似的东西

    ^(?!Select a value).*$

Which would match everything that does NOT start with "Select a value."

这将匹配所有不以“选择值”开头的内容。

#2


2  

Instead of thinking about a negative regular expression, make a positive one and negate the condition:

而不是考虑消极的正则表达式,做出积极的正则表达式并否定条件:

if (! /^Select an account$/.test(mystring)) {
  // String is not "Select an account"
}

But you don't need regex for this anyway:

但是你无论如何都不需要正则表达式:

if (mystring != 'Select an account') {
  // String is not "Select an account"
}

Or if you want "contains":

或者如果你想要“包含”:

if (mystring.indexOf('Select an account') == -1) {
  // String does not contain "Select an account"
}

#3


0  

I may not be understanding this correctly. Wouldn't this work?

我可能没有正确理解这一点。这不行吗?

if (string != "Select a value") {
    //code here
}

I don't know why you would need regular expressions to do that, which is why I'm afraid I might not be understanding what your question is.

我不知道为什么你需要正则表达式才能做到这一点,这就是为什么我可能不理解你的问题是什么。

#4


0  

An additional solution, if you’re dealing with regex in HTML5 form validation and the pattern attribute of your input is more complex than one exact string, is as follows (javascript):

另一个解决方案,如果您在HTML5表单验证中处理正则表达式并且输入的模式属性比一个完全字符串更复杂,则如下(javascript):

function findAllNotPattern(inputField) {
    var allBut1 = inputField.getAttribute("pattern")
    var allBut2 = allBut1.slice(0, 1) + "^" + allBut1.slice(1)
    var allButRe = new RegExp(allBut2, "g")
    return allButRe
}

if you want to run it in pure javascript, you'd use:

如果你想在纯JavaScript中运行它,你会使用:

findAllNotPattern(document.getElementById("inputFieldId"))