-4

I am facing trouble with JavaScript regex to match all characters except <> and {}. Can anyone please help on this?

Sujay
  • 25
  • 1
  • 4

1 Answers1

2

You can do [^{}<>]+, which matches all characters, except for those in the square brackets, being {} and <>. Here's a snippet:

var re = /[^{}<>]+/g;

// {} and <> are not matched here
console.log("abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()-=_+?.,/\|{}<>".match(re)); 
Andrew Li
  • 55,805
  • 14
  • 125
  • 143
  • I would prefer a white list rather than specifying a set of blacklisted characters. – Sujay Sep 24 '16 at 03:58
  • Why @Sujay? Blacklist seems more practical, what's your usecase? – Andrew Li Sep 24 '16 at 03:59
  • Simple reason for that being I don't want to allow any other characters which are not there in the key board. For ex: copyright symbol, other language characters etc. I just want a complete control on the list of characters allowed – Sujay Sep 24 '16 at 04:05
  • @Sujay You can do `[chars_allowed]+`. If you want all US keyboard characters: `[\x00-\x7F]+`. – Andrew Li Sep 24 '16 at 04:17