Amit
Amit

Reputation: 1491

regex for not allowing <> special characters

HI All I am using regex for not allowing special characters

I want to allow \ / * ? % | : , ( ) - _ ; # . + characters only. Its working fine for all except <>.

Can any one help me with this. May be I am doing some mistake in my code.

Thanks here is my code:

public validate(val: any) {
    let regExp = /^[ \\/*?%|()-_;#.+:, a-zA-Z0-9]+$/;
    //if (!val.match(regExp) || val.length < 1) 
    if (!regExp.test(val) || val.length < 1) 
      return false;
      else
      return true;

  }

Upvotes: 1

Views: 73

Answers (1)

g-dg
g-dg

Reputation: 283

With the escaping of the - as mentioned in the comment by @georg on your question, it would be:

let regExp = /^[ \\/*?%|()\-_;#.+:, a-zA-Z0-9]+$/;

Apparently, you may need to also escape the / character inside:

let regExp = /^[ \\\/*?%|()\-_;#.+:, a-zA-Z0-9]+$/;

Upvotes: 1

Related Questions