For JavaScript you want /[+=<>()%*|]|\!=|-{2}/
, usage:
new RegExp(/[+=<>()%*|]|!=|-{2}/).test('this!=that');
And in PHP '/[+=<>()%*|]|!=|-{2}/'
, usage:
preg_match('/[+=<>()%*|]|!=|-{2}/','this!=that');
There is no need to put |
(or operator) in your []
(character class) unless you want to match that specific character - this is assumed. Also note that character classes cannot contain a sequence/series of characters; you'll need to break those out and use |
to separate the phrases. Here is a breakdown of the regex:
/
- start delimiter
[+=<>()%*|]
- match any character in here (singular)
|
- matches either what is on the left (character class) or what is on the right (phrase)
!=
- match exactly != (phrase)
|
- again, matches either what is on the left (phrase) or on the right (phrase)
-{2}
- matches the hyphen exactly twice (phrase with a quantifier)
/
- end delimiter
From the high level, it can be interpreted as follows:
A|B|C
, either A or B or C match
- A is then
[D]
where any character D
matches
- D is then
+=<>()%*|
- B is then
!=
- C is then
E{2}
or EE
(identical because of the quantifier {n}
).
- E is then
-
Now with your variables and regex instantiation style:
JS:
var pattern = /[+=<>()%*|]|!=|-{2}/;
if( pattern.test(mystring) )
{
console.log(...);
}
PHP:
$pattern = '/[+=<>()%*|]|!=|-{2}/';
if ( preg_match($pattern,$mystring) )
{
var_dump(...);
}
Bonus: Take a look at What special characters must be escaped in regular expressions? in regards to what characters need to be escaped and in what contexts - in your case, none of them, but still a handy reference!