HOW TO EXTRACT SUB STRING BETWEEN TWO STRING PATTERN USING JAVASCRIPT/JQUERY

Ankita Pandey

Total Post:183

Points:1285
Posted by  Ankita Pandey
 1144  View(s)
Ratings:
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

  1. Royce Roy

    Post:149

    Points:1043
    Re: How to Extract Sub string between two string pattern using Javascript/JQuery

    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' ]

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!