如何使正则表达式变成非贪婪的?
我正在使用 jQuery。我有一个带有特殊字符块(开始和结束)的字符串。我想从该特殊字符块中获取文本。我使用正则表达式对象进行字符串内查找。但是,当有两个或更多特殊字符时,如何告诉 jQuery 查找多个结果呢?
我的 HTML:
<div id="container">
<div id="textcontainer">
Cuộc chiến pháp lý giữa [|cơ thử|nghiệm|] thị trường [|test2|đây là test lần 2|] chứng khoán [|Mỹ|day la nuoc my|] và ngân hàng đầu tư quyền lực nhất Phố Wall mới chỉ bắt đầu.
</div>
</div>
和我的 JavaScript 代码:
$(document).ready(function() {
var takedata = $("#textcontainer").text();
var test = 'abcd adddb';
var filterdata = takedata.match(/(\[.+\])/);
alert(filterdata);
//end write js
});
我的结果是:[|cơ thử|nghiệm|] thị trường [|test2|đây là test lần 2|] chứng khoán [|Mỹ|day la nuoc my|] .但这不是我想要的结果:(。如何获取times 1的[text]和times 2的[demo]?
我刚刚在互联网上搜索信息后完成了工作^^。我编写了这样的代码:
var filterdata = takedata.match(/(\[.*?\])/g);
- 我的结果是:[|cơ thử|nghiệm|],[|test2|đây là test lần 2|] 这是对的!但我不太明白这一点。你能回答我的原因吗?
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?
My HTML:
<div id="container">
<div id="textcontainer">
Cuộc chiến pháp lý giữa [|cơ thử|nghiệm|] thị trường [|test2|đây là test lần 2|] chứng khoán [|Mỹ|day la nuoc my|] và ngân hàng đầu tư quyền lực nhất Phố Wall mới chỉ bắt đầu.
</div>
</div>
and my JavaScript code:
$(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|đây 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 ?
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|đây là test lần 2|]
this is right!. but I don't really understand this. Can you answer my why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
非贪婪的正则表达式修饰符就像它们的贪婪的对应部分一样,但紧随其后的是
?
:The non-greedy regex modifiers are like their greedy counter-parts but with a
?
immediately following them:你是对的,贪婪是一个问题:
如果你想匹配
A--Z
,你必须使用A.*?Z
(?
使*
“不情愿”,或懒惰)。不过,有时有更好的方法可以做到这一点,例如,
这使用否定字符类和所有格量词,以减少回溯,并且可能更有效。
在你的情况下,正则表达式将是:
不幸的是 Javascript正则表达式不支持所有格量词,所以你只需要这样做:
另请参阅
快速摘要
请注意,不情愿和所有格量词也适用于有限重复
{n,m}
结构。Java 中的示例:
You are right that greediness is an issue:
If you want to match both
A--Z
, you'd have to useA.*?Z
(the?
makes the*
"reluctant", or lazy).There are sometimes better ways to do this, though, e.g.
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:
Unfortunately Javascript regex doesn't support possessive quantifier, so you'd just have to do with:
See also
Quick summary
Note that the reluctant and possessive quantifiers are also applicable to the finite repetition
{n,m}
constructs.Examples in Java:
我相信
最后的
g
意味着全局,所以它不会在第一个匹配处停止。I believe it would be like this
the
g
at the end means global, so it doesn't stop at the first match.