Reputation: 55293
I'm using the following regex to match anything between *
:
/\*([^*]*)\*/g
It should match this:
*text*
*text*
*text text
text*
The match messes up if there are * * *
in a line:
*text*
* * *
*text*
*text text
text*
What's the simplest way to prevent * * *
from messing up the match? In other words, not matching * * *
?
Upvotes: 0
Views: 82
Reputation: 13431
A capturing regex like ... /[*\s+]*\*([^*]+)\*/g ... executed by matchAll
and an additional map
ping does the job ...
const sampleText = `*foo*
* * *
*bar*
*baz* *biz**buz*
*foo bar
baz**biz
buz*`;
// [https://regex101.com/r/maKxyJ/1]
const regX = (/[*\s+]*\*([^*]+)\*/g);
console.log(
[...sampleText.matchAll(regX)].map(([match, capture]) => capture)
);
Upvotes: 2
Reputation: 163457
You could match at least a single non whitespace char other than *
in between.
\*[^*]*[^\s*][^*]*\*
Upvotes: 3
Reputation: 1170
Does [\* ]*([^*]*)\*
work for you?
https://regex101.com/r/q7kYIQ/1/
Upvotes: 1