admin管理员组文章数量:1406060
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.
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.
Share Improve this question edited Jun 19, 2017 at 13:00 Olian04 6,8922 gold badges30 silver badges56 bronze badges asked Jun 19, 2017 at 12:00 user1876040user1876040 4411 gold badge8 silver badges18 bronze badges 2- Do you want a string (or an array) without duplicates? – Casimir et Hippolyte Commented Jun 19, 2017 at 12:12
- Use an online tool like regex101 to check your regex. – evolutionxbox Commented Jun 19, 2017 at 12:21
1 Answer
Reset to default 7You can simplify it by using reverse regex and replace all allowed characters by empty string so that output will have only not-allowed characters left.:
var re = /[\w .@,\r\n*=:;&()'\/-]+/g
var input = '123.@&-_()/*=:/\';#$%^"~!?[]av'
var input = input.replace(re, '')
console.log(input);
//=> "#$%^"~!?[]"
Also note that many special characters don't need to be escaped inside a character class.
本文标签: regexGet all characters not matching the Reg expression Pattern in JavascriptStack Overflow
版权声明:本文标题:regex - Get all characters not matching the Reg expression Pattern in Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744957851a2634466.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论