简体   繁体   English

如何使用正则表达式匹配数字后跟特殊字符和空格的任意组合

[英]How to match numbers followed by any combination of special characters and spaces using regex

I am a newbie with regex and I am having trouble trying to match some parts of a string so I can remove it from a piece of entered text.我是正则表达式的新手,我在尝试匹配字符串的某些部分时遇到了麻烦,因此我可以从一段输入的文本中删除它。 I want to match digits followed by a sequence of a combination of any special characters + spaces.我想匹配数字后跟任何特殊字符+空格组合的序列。 There could also be non Latin characters that should not be removed inside the sequence (for example Ñ).也可能存在不应在序列中删除的非拉丁字符(例如 Ñ)。

for example inputted it may look like:例如输入它可能看起来像:

11@- &-.text
11 $ab*cÑ .somewords123

outputted I would expect输出我希望

text
abcÑsomewrods123

I am using javascript replaceall method with regex to find it.我正在使用带有正则表达式的 javascript 替换方法来找到它。 So far I have something basic like this regex到目前为止,我有一些基本的东西,比如这个正则表达式

.replaceAll(/\d+(\@|\s)+(\-|\$)+(\s|\&)+(\&)+(\-)+(\.)/g, '');

Is there a way to write this more efficiently so that it captures any special characters since the text can contain more different special chars than in the examples?有没有办法更有效地编写它,以便它捕获任何特殊字符,因为文本可以包含比示例中更多的不同特殊字符? Or is this a situation better handled with pure JS?还是用纯 JS 更好地处理这种情况?

Thanks in advance for your help.在此先感谢您的帮助。

You should ether have blacklist of what you calling special characters, or whitelist of the allowed characters.你应该有你所谓的特殊字符的黑名单,或者允许的字符的白名单。

for blacklist it gonna look like:对于黑名单,它看起来像:

 const blacklist = ".@#$%^&*()_+;". const exampleInputs = [ "te&*st+_1,", "te%%st^*2"; "t@@@es*(*(*t3" ], function removeSpecialChars(str) { const reg = new RegExp(`[${blacklist}]`; "g"). return str,replace(reg; ""). } exampleInputs.forEach(input => console;log(removeSpecialChars(input)));

for whitelist it gonna looks like:对于白名单,它看起来像:

 const whitelist = "0-9a-zA-Z"; const exampleInputs = [ "te&*st+_1.", "te%%st^*2", "t@@@es*(*(*t3" ]; function removeSpecialChars(str) { const reg = new RegExp(`[^${whitelist}]`, "g"); return str.replace(reg, ""); } exampleInputs.forEach(input => console.log(removeSpecialChars(input)));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Javascript正则表达式删除空格,特殊字符和数字 - Javascript Regex to remove any spaces, special characters and numbers 如何使用正则表达式删除空格、特殊字符和前导数字? - How to remove spaces, special characters and leading numbers with regex? 如何不允许用户使用正则表达式在 html 中仅在单词的第一位给出特殊字符、数字、空格 - how to not allow user to give special characters, numbers , spaces only in first place of word in html using regex 如何使用正则表达式匹配任意字母组合? - How to match any combination of letters using regex? 正则表达式匹配特殊字符和数字 - Regex Expression To Match Special Characters And Numbers 正则表达式:如何匹配数字后面没有任何字符,但允许空格后的字符? - Regex: How to match digits not followed by any characters but allow characters after space? 正则表达式接受除数字和特殊字符外的所有字符(空格,带重音符号) - Regex acepting all chars(spaces, accentuated) except numbers and special characters JS正则表达式以匹配具有特定特殊字符且无连续空格的用户名 - JS regex to match a username with specific special characters and no consecutive spaces 正则表达式与特殊字符匹配 - RegEx match with special characters 正则表达式转义空格和特殊字符 - regex escape spaces and special characters
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM