0

I have this example for pattern in HTML pattern="[a-zA-Z0-9-_. ]{1,30}". This means that only letters numbers and -_. are allowed, but I want to make a pattern like in the preg_replace which allows everything but not [ <>/\:*?"|].

So how can I go with this?

Fabrício Matté
  • 69,329
  • 26
  • 129
  • 166
user2303038
  • 45
  • 2
  • 9

3 Answers3

1

You can try the following:

$test = "Hello [<>/\:*?\"|Mister] Jean-Samuels, how are you?_.";
echo preg_replace('/[^\d\w-_. ]+/', '', $test);

Output:

Hello Mister Jean-Samuels how are you_.

Edit:

Here is a JSFiddle of your HTML Pattern.

HTML:

<input title='Enter anything but <, >, /, \, :, *, ?, ", |' 
type='text' 
pattern='[^<>\/\\:*?"|]{1,30}' required />
AdamM
  • 70
  • 1
  • 6
  • HI everyone, tahnks for the answers but i'm not talking about a regex in php code i'm talking about putting a regex in the HTML pattern i tried before to use the char ^ to exclue some letters but it didint worked in HTML pattern ... – user2303038 Apr 24 '13 at 11:42
0

The ^ operator in a character class will negate the entire class, so you can use:

[^<>/\:*?"|]

Which will match everything except those characters inside the character class. Keep in mind that you might have to escape the " if the surrounding quotes for the pattern attribute are " as well.

Vivin Paliath
  • 94,126
  • 40
  • 223
  • 295
0

Negative character classes in regex are done like this: [^characters to exclude]

Note the ^ just inside the [, that's what makes it a negative.

That said, you should probably stick with what you have, otherwise you'll get someone like me trying to enter ヴィックサ for a name :p

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592