0

I have a string like that:

var x = _something_ somethingelse _whatever_

I'd like to match by regular expressions only something and whatever words (because they are located between two underscore characters directly). Rest word is between underscore characters by accident - I don't need it. The regular expression I use is matches all of them. How to fixed that? My curent regexp:

/_(.*)_/gm

1 Answers1

0

You could search for a word boundary and then for underscore.

var x = '_something_ somethingelse _whatever_ foo_bar _foo_2';

console.log(x.match(/\b_[^_]+_\b/g));
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392