I have below requirement where a entered text must match any of below allowed character list and get all characters not matching the reg exp pattern.
- 0-9
- A-Z,a-z
And special characters like:
- space,.@,-_&()'/*=:;
- carriage return
- end of line
The regular expression which I could construct is as below
/[^a-zA-Z0-9\ \.@\,\r\n*=:;\-_\&()\'\/]/g For an given example, say input='123.@&-_()/*=:/\';#$%^"~!?[]av'. The invalid characters are '#$%^"~!?[]'.
Below is the approach I followed to get the not matched characters.
1) Construct the negation of allowed reg expn pattern like below.
/^([a-zA-Z0-9\ \.@\,\r\n*=:;\-_\&()\'\/])/g (please correct if this reg exp is right?)
2) Use replace function to get all characters
var nomatch = ''; for (var index = 0; index < input.length; index++) { nomatch += input[index].replace(/^([a-zA-Z0-9\ \.@\,\r\n*=:;\-_\&()\'\/])/g, ''); } so nomatch='#$%^"~!?[]' // finally But here the replace function always returns a single not matched character. so using a loop to get all. If the input is of 100 characters then it loops 100 times and is unnecessary.
Is there any better approach get all characters not matching reg exp pattern in below lines.
- A better regular expression to get not allowed characters(than the negation of reg exp I have used above)?
- Avoid unnecessary looping?
- A single line approach?
Great Thanks for any help on this.