简体   繁体   中英

Javascript and PHP Regular Expression to exclude different chars

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 !

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 = /[+=<>()%*|]|(!=)|(--)/;

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!

您需要使用反斜杠转义特殊字符(例如,括号)。

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 az or 0-9 ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM