1

I want to use the PHP function preg_match and the JS function test to exclude strings with this chars: |, +, --, =, <, >, !=, (, ), %, *

Can you give me the correct pattern for this expression ?

Now i use this solution:

    pattern = /[+|=|<|>|(|)|%|*]/;
    if( pattern.test(mystring) )        
    {
        alert(.....);   
    }

But it doesn't work if i use:

pattern = /[+|=|<|>|(|)|%|*|--|!=]/;
if( pattern.test(mystring) )        
{
 alert(....);
}

because it doesn't accept a singular - or !

  • Anytime you feel stuck with RegEx, head over to http://jsregex.com/ for some practice and for review check out https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/RegExp?redirect=no – Deryck Nov 30 '13 at 18:03

4 Answers4

1

First, you don't need the | if you're using [ ] - the square brackets imply a match against any one of the characters inside.

To include the - just quote it with \:

var pattern = /[+=<>()%*\-!=|]/;

edit — I was overly hasty in reading your question. To deal with != and --, those will have to be broken out into separate sub-patterns:

var pattern = /[+=<>()%*|]|(!=)|(--)/;
Pointy
  • 405,095
  • 59
  • 585
  • 614
1

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!

Community
  • 1
  • 1
zamnuts
  • 9,492
  • 3
  • 39
  • 46
  • thank you very much! it works! but you forget that i want to exclude the char "|" now this the solution thanks of your advice: pattern = /[+=<>()%*\|]|!=|-{2}/; – Gabriele Mandalari Nov 30 '13 at 21:37
  • @GabrieleMandalari oops! I added `|` to the character class. No need to escape it with a `\ `. Only `^-]\ ` must be escaped inside `[]`. – zamnuts Nov 30 '13 at 21:43
0

You need to escape special chars (for example, parenthesis) using backslash.

eRIZ
  • 1,477
  • 16
  • 32
0

Your pattern is incorrect in both cases. If you want to use a regular expression to test strings containing those characters just use [+=<>()|&!*-]. This is how you write a character group--you don't put bars between each character. If you want your group to contain -, always place that last (otherwise it might be confused as part of a character range like a-z or 0-9).

Iguananaut
  • 21,810
  • 5
  • 50
  • 63