简体   繁体   English

正则表达式在应返回true时返回false

[英]regex returns false when should return true

I have this regex: 我有这个正则表达式:

regex = new RegExp("\${")

and this string: 和这个字符串:

path = "${wf:conf('oozie.wf.application.path')}/workflows/core"

When I test the string against the regexp: 当我针对正则表达式测试字符串时:

regex .test(path)

it returns false, why? 它返回假,为什么呢?

You regex is equivalent to ${ . 您的正则表达式相当于${ Ie you are looking for the end of the string followed by a { which will never match. 也就是说,您正在寻找字符串的结尾,后跟一个{永远不会匹配。

Just like in regular expressions, you use \\ in string literals to escape special characters or create special escape sequences. 就像在正则表达式中一样,您可以在字符串文字中使用\\来转义特殊字符或创建特殊的转义序列。 Eg 例如

> 'foo\'bar'
"foo'bar"

However, if you are trying to escape a character that is not special or creating an invalid escape sequence, the backslash is just discarded: 但是,如果您尝试转义非特殊字符或创建无效的转义序列,则反斜杠将被丢弃:

> 'foob\ar'
"foobar"

That means, if you use a string literal to construct the regular expression, and you want to "forward" the backslash to the expression, you have to escape it as well: 这意味着,如果您使用字符串文字来构造正则表达式,并且想要将反斜杠“转发”到该表达式,则还必须对其进行转义:

new RegExp("\\${")

You can just create the expressions in the console and convince yourself: 您可以只在控制台中创建表达式并说服自己:

> RegExp("\${")
/${/

> /${/
/${/

> RegExp("\\${")
/\${/

> /\${/
/\${/

It really is simpler to use a regex literal: 使用正则表达式文字确实更简单:

var regex = /\${/;

You need to use double \\ to create \\${ as \\ is the string literal escape character. 您需要使用double \\来创建\\${因为\\是字符串文字转义符。

You need a string \\${ as the regex, in string literal to represent a \\ , you need to use \\\\ as a single \\ will act like a escape notation. 您需要一个字符串\\${作为正则表达式,用字符串文字表示一个\\ ,您需要使用\\\\作为单个\\就像一个转义符号。

regex = new RegExp("\\${")

Can you please explain why should I use double slash? 您能解释一下为什么要使用双斜杠吗? I use one to escape $ , why second? 我用一个逃脱$ ,为什么第二个?

Let us take another example. 让我们再举一个例子。 "\\n" is an escape-sequence in a string, producing a newline character. "\\n"是字符串中的转义序列,产生换行符。 In order to produce the sequence \\n (backslash, then n ) in a string, you need to escape the backslash: "\\\\n" . 为了在字符串中产生序列\\n (反斜杠,然后是n ),您需要转义反斜杠: "\\\\n" To produce a regular expression that matches the string \\n (backslash, n ), you can't use new RegExp("\\\\n") , since that will make a new regexp of \\ and n (result of string escaping); 要生成与字符串\\n (反斜杠, n )匹配的正则表达式,不能使用new RegExp("\\\\n") ,因为这将使新的正则表达式为\\n (字符串转义的结果); but regexp again interprets \\ and n as newline. 但是regexp再次将\\n解释为换行符。 So you need double escape: once to tell string that you want a literal backslash, and once more to convince regexp that you want a literal backslash as well. 因此,您需要双重转义:一次告诉字符串您想要一个文字反斜杠,再一次说服regexp您也想要一个文字反斜杠。 So, to match the backslash followed by n , you need to do this: 因此,要匹配后跟n的反斜杠,您需要执行以下操作:

new Regexp("\\\\n")

which is equivalent to 相当于

/\\n/

which will match the string in foo if 这将匹配foo中的字符串,如果

console.log(foo)
// => \n

In your example, it's not the backslash that is escaped, but a dollar; 在您的示例中,逃脱的不是反斜杠,而是美元。 only regexp needs the escape on that. 只有regexp才需要转义。 But you need to be sure that the backslash you escape the dollar with survives the string literal's escape mechanism: 但是,您需要确保使用反斜杠来逃避美元时能承受字符串文字的逃避机制:

"\\$"

will contain two characters: backslash and $ , and regexp constructed form it will be equivalent to /\\$/ . 将包含两个字符:反斜杠和$ ,以及正则表达式构造形式,它等效于/\\$/ But if you start with the string "\\$" , it is equivalent to "$" , since dollars don't need escaping in regular strings; 但是,如果您以字符串"\\$"开头,则等效于"$" ,因为美元不需要在常规字符串中转义。 and a regexp made from that will match the end of the line, and not a dollar sign. 以及由此产生的正则表达式将与行尾匹配,而不是美元符号。

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

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