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

Total Post:183

Posted on    January-20-2015 10:09 PM

 JavaScript JQuery  Regex 

 1 Reply(s)
 1091  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:149

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)) {




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

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

Follow MindStick