0

Code:

<script language="javascript1.2">
   var re = /^\[\[.+$/;
   var str = '[[FirstName]] [[LastName]] watched "[[$Title]]" and sent you the following               
             comment and link:[[iaf_message/5]][[#if iaf_message/6 
              No]]http://webcasts.advanstar.com/acc/iaf/1/[[$PROGRAMID]]/Clicking the 
             URL above will take you to the webcast: "[[$Title]]" 
             [[#else]]http://webcasts.advanstar.com/acc/iaf/1/[[$PROGRAMID]]/?
              _IAFSegment=[[$SEGMENT]]&_IAFTime=[[$posmmss]]Clicking the URL above will 
              take you to the webcast "[[$Title]]" at the point in the program that 
              [[/User/FirstName]] thought would be most relevant to you.
              [[#endif]]Message from Advanstar Communications[Sender IP: [[$IP]]] If 
              you believe you have received this email in error, or for customer 
              service at Accela Communications, please send a message to 
              mailto:support.getinfoadvanstar.com';

  var myArray = str.match(re);
  console.log(myArray);
</script>

I want to fetch all values which are all present inside [[ ]] in an array using regular expression. Please help me.

1
  • 2
    please copy and paste the code as it is. Commented Jun 30, 2014 at 14:18

2 Answers 2

1

You can do this:

var myArray = str.match(/\[\[.+?\]\]/g).map(
    function(x){ return x.replace(/\[|\]/g,"");
});
Sign up to request clarification or add additional context in comments.

3 Comments

@Shanmugam glad to have helped.
@Shanmugam don't forget to mark it as answer by clicking on the tick mark
If you can't use .map, use a library like jquery or underscore which provides .map. However, most HTML5-enabled browsers support .map nowadays, althought many companies have windows XP installed and use internet explorer 8, so don't have default .map function.
1

This should work in all versions of JavaScript:

var mystring = "[[foo]] .. [[bar]] ... [[baz]]";
var pattern = /\[\[([^\]]*)\]\]/g
var array = [];
match = pattern.exec(mystring);
while (match !== null) {
  array.push(match[1]);
  match = pattern.exec(mystring);
}

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.