Javascript regex: match any character that is not in the set of symbols (but only if this symbol doesn't follow a backslash)

Viewed 39

I need a regex which matches any character that is not in the set of symbols (but only if this symbol doesn't follow a backslash)

Is there any one line solution?

sample input: "Test1\,test2,test3\<\>test4<>test5"
reqex to improve: /[^,<>]+/

expected output: enter image description here

1 Answers

Assuming \ is not at the first position of a match you may use this regex:

[^,<>\\]+(?:\\.[^,<>\\]*)*

RegEx Details:

  • [^,<>\\]+: Match 1 or more of any character that are not inside the the [...]
  • \\. match any escaped character

RegEx Demo

If \ can be at first position then you may use:

(?:\\.|[^,<>\\]+)+

RegEx Demo 2

Related