1

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

1
  • 1
    Can you show your code? Commented Jan 21, 2015 at 2:00

2 Answers 2

1

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' ]
Sign up to request clarification or add additional context in comments.

1 Comment

You're welcome. Next time when a question like this is asked please consider posting what you have attempted to try and solve the problem.
0

Use capturing groups.

\[\[tag::(.*?)]]

Get the value you want from group index 1. .*? will do a non-greedy match of zero or more characters. [ is a regex meta character which denotes the start of a character class. So you need to escape [ in your regex in-order to match a literal [ symbol.

DEMO

> var str = "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.\nLorem 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.\n[[tag::4797800]] [[tag::4797769]]"
> var re = /\[\[tag::(.*?)]]/g;
> while (m = re.exec(str)) {
... match.push(m[1]);
... }
> console.log(match)
[ '4797897', '4797800', '4797769' ]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.