1

I'm trying to match every > and >> not surrounded by single or double qoutes.

var a = 'hello > you'; // true
var b = 'hello >> you'; // true
var c = '"hello > you"'; // false
var d = '"hello > you" >> you'; // true
var e = "'hello' > you"; // true
var f = "'hello > you'"; // false

I have been working around for some time but have not come op with anything useful.

Andreas Louv
  • 46,145
  • 13
  • 104
  • 123
  • Sounds vaguely similar to [this](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454). That said, lookbehind/ahead should solve it for you. – PinnyM Sep 21 '12 at 14:02
  • Regular expression sample here: http://stackoverflow.com/questions/10032447/match-regex-pattern-when-not-inside-a-set-of-quotes-text-spans-multiple-lines – Lukos Sep 21 '12 at 14:02

1 Answers1

2

If you're only trying to check that the string matches, you could use something like this:

var regex = /^(?:"[^"]*"|'[^']*'|[^"'>]*)*>/;

This checks to make sure that, from the beginning, the string has either a quoted section (single or double) or other characters that are not quotes or the greater than sign, followed by a greater-than sign.

This won't work well for nested quotes (within the string), but it should work for one layer of quotation marks within the string.

Platinum Azure
  • 45,269
  • 12
  • 110
  • 134
  • this is quite clever.. Thanks.. Is there a way to easily convert this to a splitting regex? Say: `a.split(regex)` -> `["hello ", " you"]` or `["hello ", ">", " you"]`. – Andreas Louv Sep 24 '12 at 11:35
  • I don't know about a splitting regex, but you can use capturing groups to capture the results. – Platinum Azure Nov 26 '12 at 16:30