Starkers
Starkers

Reputation: 10551

Match everything between certain characters

Wow, I suck at regex

http://regex101.com/r/lM8oX3

([*][.]+[*])

I'm trying to match text such as this:

*hello*

Upvotes: 0

Views: 46

Answers (2)

hsz
hsz

Reputation: 152294

Just try with following regex:

(\*[^*]+\*)

In your regex you have [.] which in fact searches for dots because in [] it loses its special context and is treated as a normal character. You should better use .+ then but it will match also * characters. So use my above solution then.

Live demo

Upvotes: 4

mrk
mrk

Reputation: 5127

This will capture

var text = "asdfasdf *hello*";
console.log( text.match(/([*][^*]+[*])/)[1]);

But that only grabs the first match;

If you want all matches

var text = "asdfasdf *hello* asdffdsa  *asdf*";
var matches = text.match(/([*][^*]+[*])/g);

if(matches.length > 1) {
   for(var i=1; i<matches.length; i++) {
       console.log(matches[i]);
   }
}

Upvotes: 1

Related Questions