MediaMaker
MediaMaker

Reputation: 110

javascript split regular expression

When given a string, e.g.:

daily (similar term)|day-to-day (similar term)|day-after-day (similar term)|every day (similar term)|nightly (similar term)|first-string (similar term)|lawful (similar term)|orderly (similar term)|rule-governed (similar term)|official (similar term)|prescribed (similar term)|regularized (similar term)|regularised (similar term)|routine (similar term)|standard (similar term)|stock (similar term)|timed (similar term)|uniform (similar term)|weak (similar term)|well-ordered (similar term)|rhythmical (related term)|rhythmic (related term)|symmetrical (related term)|symmetric (related term)|systematic (related term)|irregular (antonym)veritable|typical (similar term)standard (similar term)irregular (antonym)scheduled (similar term)usual (similar term)even|steady (similar term)steady|frequent (similar term)standing (similar term)|irregular (antonym)unconstipated|diarrheal (similar term)|diarrhoeal (similar term)|diarrhetic (similar term)|diarrhoetic (similar term)|diarrheic (similar term)|diarrhoeic (similar term)|lax (similar term)|loose (similar term)|constipated (antonym)even|symmetrical (similar term)|symmetric (similar term)normal (similar term)full-time (similar term)habitue|fixture|patron|frequentersoldierfollowersize

how do I create an array of synonyms, e.g.

var thisArr = ['daily', 'day-to-day', 'day-after-day', 'every day', 'nightly']

etc., but where it says 'irregular (antonym)', I don't want to include the anytonym (in this case 'irregular'). The other words (not the bracketed ones) are okay to include. But I don't want the "|" characters, or the brackets. I tried

var thisRegExp = /\|(.*?)\|/;

and I can remove the "|" to get, e.g.

"day-to-day (similar term)"

but I want to remove the bracketed things as well, and any spaces beside brackets I guess, as they would not be part of 'word pairs'. Any help much appreciated. Thanks, MediaMaker.

Upvotes: 2

Views: 83

Answers (1)

m.cekiera
m.cekiera

Reputation: 5395

You can use regex:

([^|()]+)(?= \(similar term|\||$)

DEMO

  • ([^|()]+) - one or more characters, but not |,(,) to match word beetween | to bracket
  • (?= \(similar term|\||$) - positive lookahead for (similar term), or | or end of string, to match only similar words or words without description in brackets

to get just similar (without (similar word)) or not described words.

Upvotes: 1

Related Questions