614 posts
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 3-4 years
  • Sold between 10 000 and 50 000 dollars
ChapterThemes says

The following situation:

I have an array with possible matches to find:

var posmatches = [ "one", "two", "three", "four" ];

This is example content i want to search in.

Suspendisse vestibulum [one] dignissim quam. Integer vel augue.
Phasellus nulla purus, [four attr="whatever"] interdum ac,
venenatis non, varius rutrum, leo. Pellentesque habitant morbi tristique
senectus et netus et malesuada fames.

I’m trying to find matches between square brackets, including everything until the closing bracket.

So i want a result as following:

0: [one]
1: [four attr="whatever"]

If i need to only find 1 certain match doing the same thing i already have the following working code:

var regmatch = "\\[four(.*?)\\]";
var re = new RegExp( regmatch, "img" );
var matches = content.match( re );

How could i do the same thing, but then let it put some sort of OR statement in place of ‘four’ in the regex, but matching the rest like in the regex.

I’ve tried the following for use in regex…

var ormatches = posmatches.join("|");

..., and this works using it inside the regex, but i want to include the square brackets like above.

Anyone? :)

114 posts
  • Elite Author
  • Sold between 100 000 and 250 000 dollars
  • Exclusive Author
  • Bought between 100 and 499 items
  • Referred between 50 and 99 users
  • Most Wanted Bounty Winner
  • Envato Studio (Microlancer) Beta Tester
  • Has been a member for 2-3 years
nagaemas says

Try this ;)

/(\[(one|two|three|four)(?![\w-])[^\]\/]*\])/
767 posts
  • Author had a Free File of the Month
  • Bought between 50 and 99 items
  • Exclusive Author
  • Has been a member for 4-5 years
  • Sold between 1 000 and 5 000 dollars
outandouter says

How about this?

var regmatch = "\\[(" + postmatches.join("|") + ")(.*?)\\]";
614 posts
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 3-4 years
  • Sold between 10 000 and 50 000 dollars
ChapterThemes says

@Nagaemas: came close with a couple of tweaks.

@Outandouter: Perfect! How do you come up with such a thing so fast? And so short, mine was starting to get longer and longer.

Could you explain the regex? Damn it looks so simple but i didn’t figured it out.

thank you very very much!

767 posts
  • Author had a Free File of the Month
  • Bought between 50 and 99 items
  • Exclusive Author
  • Has been a member for 4-5 years
  • Sold between 1 000 and 5 000 dollars
outandouter says

@Nagaemas: came close with a couple of tweaks.

@Outandouter: Perfect! How do you come up with such a thing so fast? And so short, mine was starting to get longer and longer.

Could you explain the regex? Damn it looks so simple but i didn’t figured it out.

thank you very very much!

I just combined two of your solutions into one.

In the code that I posted, putting the return value of postmatches.join() and concatenating the strings, you get the following regex string:

"\\[(one|two|three|four)(.*?)\\]"

Yours was:

"\\[four(.*?)\\]"

So, the result is pretty much the same as your original regex with one exception that “four” is now grouped with “one”, “two” and “three” and they have alternation character amongst them.

Hope this helps!

614 posts
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 3-4 years
  • Sold between 10 000 and 50 000 dollars
ChapterThemes says

Sounds so logical. In fact, i think i tried it before, but i see now i forgot the () around the one|two|three values, and the regex failed. But now it’s working perfectly.

Thx again.

by
by
by
by
by
by