---
title: "How to Extract Sub string between two string pattern using Javascript/JQuery"  
description: "How to Extract Sub string between two string pattern using Javascript/JQuery"  
author: "Anonymous User"  
published: 2015-01-20  
updated: 2015-01-20  
canonical: https://www.mindstick.com/forum/12883/how-to-extract-sub-string-between-two-string-pattern-using-javascript-jquery  
category: "javascript"  
tags: ["jquery", "regex"]  
reading_time: 1 minute  

---

# How to Extract Sub string between two string pattern using Javascript/JQuery

I am new to [regular expression](https://www.mindstick.com/articles/11909/java-regex-or-regular-expression-in-java). I need to extract between two [string](https://www.mindstick.com/articles/1527/string-split-in-c-sharp) [pattern](https://www.mindstick.com/forum/2314/what-is-the-mvp-and-mvc-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](https://www.mindstick.com/forum/1830/extract-text-from-opened-microsoft-word-document) between **[[tag::** and **]]** , the expected [output](https://www.mindstick.com/interview/34427/explain-the-output-in-angular) should be 4797897 , 4797800 and 4797769

I am using [Javascript](https://www.mindstick.com/articles/874/how-to-create-watermark-text-for-textbox-by-using-javascript) and JQuery

Can some please help me . Thanks in [advance](https://www.mindstick.com/blog/33258/jee-mains-and-jee-advance-exams)

## Replies

### Reply by Royce Roy

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' ]
```


---

Original Source: https://www.mindstick.com/forum/12883/how-to-extract-sub-string-between-two-string-pattern-using-javascript-jquery

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
