2

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.

3
  • How about /\s*\(.*?\)\|?/? Commented Sep 18, 2015 at 13:22
  • what with typical (similar term)standard (similar term)irregular (antonym)scheduled (similar term)usual (similar term)even? there is not |, do you want it to match or not? Commented Sep 18, 2015 at 13:48
  • Hi Biffen, this works well for me with the split function, but on the 35th item, suddenly it has "symmetric (similar term)normal (similar term)full-time (similar term)habitue|fixture|patron|frequentersoldierfollowersize". It is unfortunate that the original string formating is not consistent, I think that's why it's doing this. Thanks Commented Sep 18, 2015 at 13:49

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Hi m.cekiera, I got this working (sort of) with the split function. It seems to generate a term for every second array item, the alternates being what was filtered out. Or am I missing something? Thanks.
@MediaMaker sorry, my miskate, this regex is for match() not split() method, with split() you will get reversed effect, I will try to make regex for split()
I added the 'g' as per your demo and used it with match and ir works great! Thanks :) Here is what I used /([^|()]+)(?= (similar term|\||$)/g

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.