-1

I have a string of html contained in between [marquee][/marquee] tags in the string like so:

var s = '[marquee]<p>some html here 1</p>[/marquee][marquee]<p>some html here again 2</p>[/marquee]';

what I would like to do is match each one of these marquees to create an array with each marquee as an array element.

What I've tried so far is:

var marqueeArray = s.trim().match(/\[marquee\][\s\S]*\[\/marquee\]/g);

but all i get back is and array with the length of 1. both marquees end up being included as one string like this:

["[marquee]<p>some html here 1</p>[/marquee][marquee]<p>some html here again 2</p>[/marquee]"]

what i would like is this:

["[marquee]<p>some html here 1</p>[/marquee]", "[marquee]<p>some html here again 2</p>[/marquee]"]

eventually i will need to remove the brackets ([marquee] and [/marquee]) from each element and replace with and join one big string for use in the application. any point in the right direction on that would be great as well.

James
  • 43
  • 3

2 Answers2

2

You only have to use ? quantifier to make your search lazy.

Add ? as mentioned \[marquee\][\s\S]*?\[\/marquee\]

Regex101 Demo

Rahul
  • 2,658
  • 12
  • 28
1

The following will match all marquee tags and the content between them.

s.match(/(\[marquee\].*?\[\/marquee\])/g);

If you want to capture the content between the marquee tags you can use the following:

var pattern = /\[marquee\](.*?)\[\/marquee\]/g;
var match = pattern.exec(s);
while(match != null){
  console.log(match[1]); // Prints out content between tags
  match = pattern.exec(s);
}
spencer.sm
  • 19,173
  • 10
  • 77
  • 88