0

I'm completely new to regexps and syntax melts my brain, so basically i have a string which looks like this

randomtext WORD randomtext WORD neededtext BORDERWORD randomtext

Where word and borderword are different from each other, other stuff is self explanatory.

I've got as far as /(.?(WORD)){2}((.|\n)*)BORDERWORD/ but it doesn't work.

Another problem is that both randomtext and neededtext contain newline characters, which i think i solved in the matching group that tries to match neededtext ((.|\n)*) but have no idea how to make it work in the first one.

Any help will be greatly appreciated.

Edit: figured out a workaround, first match word((.|\n)*)borderword and then match the result with word((.|\n)*) Done. Doesn't seem right but it works for my purposes.

mouzfun
  • 1
  • 2

2 Answers2

1

This should work:

\bWORD\s*((?:(?!WORD)(?:.|\n))*?)\s*BORDERWORD\b

It makes sure that you want to extract text between WORD and BORDERWORD, and that text does not contain another WORD sequence.

Capturing group 1 will contain the needed text

Regex demo

James Buck
  • 1,640
  • 9
  • 13
  • Damn, there is some weird stuff happening with cyrillic text i think. Your example with swapped words for my actual ones [https://regex101.com/r/lD6pC7/3](https://regex101.com/r/lD6pC7/3) My actual string with words swapped [https://regex101.com/r/lD6pC7/2](https://regex101.com/r/lD6pC7/2) – mouzfun Jun 18 '16 at 19:32
  • @mouzfun You're going to have issues with certain unicode chars in JavaScript regex. Take a look [here](http://stackoverflow.com/a/280762/4959722) for some possible solutions. – James Buck Jun 18 '16 at 19:42
  • Damn, at this point i think it might be easier to use .split. Also it matches fine with `word((.|\n)*)borderword` , it's that damned first occurance that ruins everything, maybe there is some workaround? Also, randomtext and neededtext can't contain both word and borderword – mouzfun Jun 18 '16 at 20:10
  • figured it out, first match `word((.|\n)*)borderword` and then match the result with `word((.|\n)*)` Done. Doesn't seem right but it works for my purposes. – mouzfun Jun 18 '16 at 20:27
0
var str = "randomtext WORD randomtext WORD neededtext BORDERWORD randomtext";
var matches = str.match(/^.+? WORD .+? WORD (.+?) BORDERWORD/);
console.log(matches[1]); // neededtext
Billy Moon
  • 57,113
  • 24
  • 136
  • 237