0

My starting string is

alpha < beta < gamma < delta < epsi

I want to capture single tokens alpha,beta, ...

In one such expression, there might be also the possibily for operators to be referenced as 'lt','gt' and I have applied this regular expression so far.

/[^(\<\=?|\>\=?|==\ )|^(eq|lt|gt)]/g

This expression does not detect 'eq', 'lt', 'gt' as bounded words, but just 'e', 'q', 'l', 't', 'g'. What am I missing ?

Sandro Rosa
  • 507
  • 4
  • 12

1 Answers1

3

Just do splitting on the boundaries you mentioned and note that use character classes only for orring each char sepearately. If you want to apply OR on group of characters then you must go with capturing group (....) or non-capturing group (?:...)

var s = 'alpha < beta < gamma < delta < epsi'
alert(s.split(/\s*(?:\b(?:eq|lt|gt)\b|[<>]=?|==)\s*/))
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
  • Good ! I can't use the split command this way, because I need a same regular expression for different needs. But I merged your solution with mine and I obtained /[^(\<\=?|\>\=?|==\ )]|^[\b(?:eq|lt|gt)\b]/g which works fine as desired. Thank you ! – Sandro Rosa Oct 26 '15 at 12:03
  • @SandroRosa: No, you have got it wrong. The `\b` in `[\b(?:eq|lt|gt)\b]` means a backspace symbol. In the suggestion, it is outside the character class, a word boundary. Well, you regex is wrong. Read my comment, check the [regular-expressions.info](http://www.regular-expressions.info/brackets.html). – Wiktor Stribiżew Oct 26 '15 at 12:12
  • Your're right: I'm a newbie with regular expressions. I will use split cmd. Your snippet still works great ! – Sandro Rosa Oct 26 '15 at 12:43