Javascript 如何使正则表达式变成非贪婪的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2824302/
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-08-23 02:04:33  来源:igfitidea点击:

How to make Regular expression into non-greedy?

javascriptregexfilterexpressionregex-greedy

提问by Rueta

I'm using jQuery. I have a string with a block of special characters (begin and end). I want get the text from that special characters block. I used a regular expression object for in-string finding. But how can I tell jQuery to find multiple results when have two special character or more?

我正在使用 jQuery。我有一个带有特殊字符块(开始和结束)的字符串。我想从那个特殊字符块中获取文本。我使用正则表达式对象进行字符串内查找。但是,当有两个或更多特殊字符时,如何告诉 jQuery 查找多个结果?

My HTML:

我的 HTML:

<div id="container">
    <div id="textcontainer">
     Cu?c chi?n pháp ly gi?a [|c? th?|nghi?m|] th? tr??ng [|test2|?ay là test l?n 2|] ch?ng khoán [|M?|day la nuoc my|] và ngan hàng ??u t? quy?n l?c nh?t Ph? Wall m?i ch? b?t ??u.
    </div>
</div>

and my JavaScript code:

和我的 JavaScript 代码:

$(document).ready(function() {
  var takedata = $("#textcontainer").text();
  var test = 'abcd adddb';
  var filterdata = takedata.match(/(\[.+\])/);

  alert(filterdata); 

  //end write js 
});

My result is: [|c? th?|nghi?m|] th? tr??ng [|test2|?ay là test l?n 2|] ch?ng khoán [|M?|day la nuoc my|]. But this isn't the result I want :(. How to get [text] for times 1 and [demo] for times 2 ?

我的结果是:[|c? th?|nghi?m|]th? tr??ng [|test2|?ay là test l?n 2|] ch?ng khoán [|M?|day la nuoc my|]. 但这不是我想要的结果:(。如何获得第 1 次的 [文本] 和第 2 次的 [演示]?



I've just done my work after searching info on internet ^^. I make code like this:

我刚刚在互联网上搜索信息后完成了我的工作^^。我制作这样的代码:

var filterdata = takedata.match(/(\[.*?\])/g);
  • my result is : [|c? th?|nghi?m|],[|test2|?ay là test l?n 2|]this is right!. but I don't really understand this. Can you answer my why?
  • 我的结果是:[|c? th?|nghi?m|],[|test2|?ay là test l?n 2|]没错!但我真的不明白这一点。你能回答我的为什么吗?

回答by Asaph

The non-greedy regex modifiers are like their greedy counter-parts but with a ?immediately following them:

非贪婪的正则表达式修饰符就像它们贪婪的对应部分,但?紧跟在它们之后:

*  - zero or more
*? - zero or more (non-greedy)
+  - one or more
+? - one or more (non-greedy)
?  - zero or one
?? - zero or one (non-greedy)

回答by polygenelubricants

You are right that greediness is an issue:

你是对的,贪婪是一个问题:

--A--Z--A--Z--
  ^^^^^^^^^^
     A.*Z

If you want to match both A--Z, you'd have to use A.*?Z(the ?makes the *"reluctant", or lazy).

如果您想同时匹配两者A--Z,则必须使用A.*?Z?使*“不情愿”或懒惰)。

There are sometimes better ways to do this, though, e.g.

不过,有时有更好的方法来做到这一点,例如

A[^Z]*+Z

This uses negated character class and possessive quantifier, to reduce backtracking, and is likely to be more efficient.

这使用否定字符类和所有格量词来减少回溯,并且可能更有效。

In your case, the regex would be:

在您的情况下,正则表达式将是:

/(\[[^\]]++\])/

UnfortunatelyJavascript regex doesn't support possessive quantifier, so you'd just have to do with:

不幸的是Javascript 正则表达式不支持所有格量​​词,所以你只需要做:

/(\[[^\]]+\])/

See also

也可以看看



Quick summary

快速总结

*   Zero or more, greedy
*?  Zero or more, reluctant
*+  Zero or more, possessive

+   One or more, greedy
+?  One or more, reluctant
++  One or more, possessive

?   Zero or one, greedy
??  Zero or one, reluctant
?+  Zero or one, possessive

Note that the reluctant and possessive quantifiers are also applicable to the finite repetition {n,m}constructs.

请注意,不情愿和所有格量词也适用于有限重复{n,m}结构。

Examples in Java:

Java 中的示例:

System.out.println("aAoZbAoZc".replaceAll("A.*Z", "!"));  // prints "a!c"
System.out.println("aAoZbAoZc".replaceAll("A.*?Z", "!")); // prints "a!b!c"

System.out.println("xxxxxx".replaceAll("x{3,5}", "Y"));  // prints "Yx"
System.out.println("xxxxxx".replaceAll("x{3,5}?", "Y")); // prints "YY"

回答by iangraham

I believe it would be like this

我相信会是这样

takedata.match(/(\[.+\])/g);

the gat the end means global, so it doesn't stop at the first match.

g末意味着全球性的,所以它不会在第一场比赛停止。