Home > DeveloperSection > Forums > How to Extract Sub string between two string pattern using Javascript/JQuery
Ankita Pandey
Ankita Pandey

Total Post:183

Points:1285
Posted on    January-20-2015 10:09 PM

 JavaScript JQuery  Regex 
Ratings:


 1 Reply(s)
 799  View(s)
Rate this:

I am new to regular expression. I need to extract between two string pattern ([[tag:: & ]]) and for each occurrence.

For Example:

If the text is :

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus [[tag::4797897]] mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu.

[[tag::4797800]] [[tag::4797769]]

As I want the extract text between [[tag:: and ]] , the expected output should be 4797897 , 4797800 and 4797769

I am using Javascript and JQuery

Can some please help me . Thanks in advance



Royce Roy
Royce Roy

Total Post:134

Points:938
Posted on    January-20-2015 10:41 PM

To achieve the desired results you can use the exec() method in a loop, pushing the match result of the capturing group to the results array.

var str = ' ... '

var re  = /\[\[tag::([^\]]+)]]/g,

matches = [];

 

while (m = re.exec(str)) {

  matches.push(m[1]);

}

 

console.log(matches) //=> [ '4797897', '4797800', '4797769' ]


Don't want to miss updates? Please click the below button!

Follow MindStick