使用 javascript 正则表达式进行正向预测

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24226953/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 02:24:14  来源:igfitidea点击:

Positive lookahead with javascript regex

javascriptregex

提问by user3718914

I have been messed up with regex.. I found it difficult for me..I have seen a code like:

我被正则表达式搞砸了..我发现这对我来说很困难..我见过这样的代码:

function myFunction() {
   var str = "Is this all there is";
   var patt1 = /is(?= all)/;
   var result = str.match(patt1);
   document.getElementById("demo").innerHTML = result;
}

When i run this code it gave me the output is.

当我运行此代码时,它给了我输出is

But when i add like /is(?=there)/it didnt output anything. I am new to regular expression ..hope you guys can help in in understanding positive lookahead in regex..I have followed many tutorials it didnt helped me.

但是当我添加 like/is(?=there)/它没有输出任何东西时。我是正则表达式的新手。

Hope you guys can help me out. Thanks!

希望大家能帮帮我。谢谢!

回答by zx81

The regex is(?= all)matches the letters is, but only if they are immediatelyfollowed by the letters all

正则表达式is(?= all)匹配字母is,但前提是它们紧跟字母all

Likewise, the regex is(?=there)matches the letters is, but only if they are immediatelyfollowed by the letters there

同样,正则表达式is(?=there)匹配字母is,但前提是它们紧跟字母there

If you combined the two in is(?= all)(?=there), you are trying to match the letters is, but only if they are immediatelyfollowed both by the letters allANDthe letters thereat the same time... which is not possible.

如果将两者结合在一起is(?= all)(?=there),则您正在尝试匹配字母is,但前提是它们紧跟同时是字母all字母there......这是不可能的。

If you want to match the letters is, but only if they are immediatelyfollowed eitherby the letters allorthe letters there, then you can use:

如果你想匹配的字母is,但只有当他们被立即跟随要么由字母all字母there,那么你可以使用:

is(?= all|there)

is(?= all|there)

If, on the other hand, you want to match the letters is, but only if they are immediatelyfollowed by the letters all there, then you can just use:

另一方面,如果您想匹配字母is,但前提是它们紧跟字母all there,那么您可以使用:

is(?= all there)

is(?= all there)

What if I want isto be followed by alland there, but anywhere in the string?

如果我想跟isalland 之后there,但在字符串中的任何地方怎么办?

Then you can use something like is(?=.* all)(?=.*there)

然后你可以使用类似的东西 is(?=.* all)(?=.*there)

The key to understanding lookahead

理解前瞻的关键

The key to lookarounds is to understand that the lookahead is an assertion that checks that something follows, or precedes at a specific position in the string. That is why I bolded immediately. The following article should dispel any confusion.

环视的关键是理解环视是一个断言,它检查字符串中特定位置后面或前面的内容。这就是为什么我立即加粗。下面的文章应该可以消除任何混淆。

Reference

参考

Mastering Lookahead and Lookbehind

掌握前瞻和后视

回答by hwnd

The Positive Lookaheadfails for the fact that theredoes not immediately follow is.

正向前查找失败的,事实there不会立即跟随

is(?=there) # matches is when immediately followed by there


To match isif therefollows somewhere in the string, you would do:

匹配ifthere跟在字符串的某个地方,你会这样做:

is(?=.*there) 

Explanation:

说明

is          # 'is'
(?=         # look ahead to see if there is:
  .*        #   any character except \n (0 or more times)
  there     #   'there'
)           # end of look-ahead

See Demo

演示

A detailed tutorial I recommend: How to use Lookaheads and Lookbehinds in your Regular Expressions

我推荐的详细教程:如何在正则表达式中使用 Lookaheads 和 Lookbehinds