1

There is a field which accepts a-zA-Z for which I am using regEx which works fine as

/^[a-zA-Z\s]+$/

Now field should also except all chars except &,<,> I have modified the regEx to support this change and new regEx is

/^[a-zA-Z\s]+[/!@#$%^&*()]+$/

but looks like its not working.... Can someone advise what is wrong am I doing ?

Also, instead of adding all allowed special characters, can we add only special characters which are not allowed in RegEX ?

================================================================================= Validating the code in typescript using below code, It should work as suggested but still getting false for all input values...

import { Component } from '@angular/core';
@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  private output ;
  private regEx = new RegExp('/^[a-zA-Z\s/!@#$%^&*()]+$/');
  private input = 'A';

  constructor(){
    console.log(this.regEx.test(this.input));
  }
 }
Amit
  • 3,358
  • 9
  • 34
  • 48
  • Does this answer your question? [Regex: allow everything but some selected characters](https://stackoverflow.com/questions/12097651/regex-allow-everything-but-some-selected-characters) – Heretic Monkey Dec 17 '19 at 19:27
  • Using a tool like Debuggex https://www.debuggex.com/r/rx7ViDYUkf9gnJar could help you understand what this does/doesn't match. In this case, you'll notice with two separate character classes this match letters then symbols, exactly in that order. – Wiseguy Dec 17 '19 at 19:29
  • Combine the classes `[a-zA-Z\s!@#$%^&*()]` Yes, you can do those that are not allowed using a negated character class `[^&<>]` – ctwheels Dec 17 '19 at 19:31
  • 1
    What do you mean by not working? Are you getting typescript (JavaScript) errors? Your regex currently states "start the string with 1 or more whitespaces or case-insensitive letters and end with one or more of the special chars. – MonkeyZeus Dec 17 '19 at 19:36
  • @MonkeyZeus - what I meant by not working was that I am able to enter the characters without any issue. – Amit Dec 17 '19 at 20:38

2 Answers2

2

Your pattern isn't working as expected because you added another character set after the first one, instead of adding additional characters to the existing set. So first the regex will look for a string of letters and whitespaces, then a string of shift+number characters. Try this instead:

/^[a-zA-Z\s/!@#$%^&*()]+$/

Also, if you really want to allow "all characters except &, <, and >", it should be pointed out that there are many other characters not contained in the above set, such as foreign alphabet characters, emojis, etc. Even some on the keyboard such as ,.;:;'"[]{}| and numerals that you left out.

If you truly want to match anything other than &, <, and >, you should simply create a negated character class with ^:

/^[^&<>]+$/

CAustin
  • 4,525
  • 13
  • 25
  • Tried running in one sample typescript file, but still not working. – Amit Dec 17 '19 at 20:46
  • Added typescript sample too in the question above for clarity, getting false for all the values I am entering. – Amit Dec 17 '19 at 20:51
2

Remove the leading and trailing slashes and escape the backslash:

private regEx = new RegExp('^[a-zA-Z\\s/!@#$%^&*()]+$');

You are passing in the regex as a string to the constructor so you need to omit the leading and trailing slashes per the docs.

You are constructing a string so escaping rules matter, you need to escape the backslash so that a literal \s will be provided to the constructor instead of just s.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp#Description

MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77