Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
338 views
in Technique[技术] by (71.8m points)

javascript - 如何使正则表达式变为非贪婪?(How to make Regular expression into non-greedy?)

I'm using jQuery.(我正在使用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查找多个结果?) 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|] .(我的结果是: [|c?th?|nghi?m|] thtr trng [| test2 |?aylàtestl?n2 |]ch?ngkhoá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 ?(但这不是我想要的结果:(。如何获取时间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!.(我的结果是: [|c?th?|nghi?m|],[| test2 |?aylàtestl?n2 |]这是对的!) but I don't really understand this.(但是我不太明白) Can you answer my why?(你能回答我为什么吗?)   ask by Rueta translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...