简体   繁体   English

如何在 javascript 中使用正则表达式删除特定字母

[英]How to remove specific letters using regex in javascript

var cartstring = "27,00 - R"

How can I remove spaces and "-" and "R" using only regex (not allowed to use slice etc.)?如何仅使用正则表达式(不允许使用 slice 等)删除空格和“-”和“R”? I need to make strings cartstring1 and cartstring2 which should both be equal to "27,00", first by removing spaces and "-" and "R", and second by allowing only numbers and ",".我需要创建都应等于“27,00”的字符串 cartstring1 和 cartstring2,首先删除空格和“-”和“R”,然后只允许使用数字和“,”。

cartstring1 = cartstring.replace(/\s/g, "");
cartstring2 = cartstring.replace(/\D/g, "");

Please help me modify these regular expressions to have a working code.请帮助我修改这些正则表达式以获得有效代码。 I tried to read about regex but still cannot quite get it.我试图阅读有关正则表达式的内容,但仍然不太明白。
Thank you very much in advance.非常感谢你提前。

you can just capture just what you are interested in number and comma:您可以只捕获您对数字和逗号感兴趣的内容:

 let re = /[\d,]+/g let result = "27,00 - R".match(re) console.log(result)

You can group the characters you want to remove:您可以对要删除的字符进行分组:

 var cartstring = "27,00 - R" let res = cartstring.replace(/(\s|-|R)/g, "") console.log(res)

Or alternatively, split the string by a space and get the first item:或者,用空格分隔字符串并获取第一项:

 var cartstring = "27,00 - R" let res = cartstring.split(" ")[0] console.log(res)

You are using 2 replacements, one replacing all whitespace chars \s and the other replacing all non digits \D , but note that \D also matches \s so you could omit the first call.您正在使用 2 个替换,一个替换所有空白字符\s ,另一个替换所有非数字\D ,但请注意\D也与\s匹配,因此您可以省略第一个调用。

Using \D will also remove the comma that you want to keep, so you can match all chars except digits or a comma using [^\d,]+ in a single replacement instead:使用\D还将删除您想要保留的逗号,因此您可以在单个替换中使用[^\d,]+匹配除数字或逗号以外的所有字符:

 var cartstring = "27,00 - R"; console.log(cartstring.replace(/[^\d,]+/g, ''));

暂无
暂无

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

相关问题 我如何使用正则表达式从字母字符串中删除特定字母 - how do i remove specific letters from alphabet string using regex 如何在标签内搜索特定属性并使用javascript正则表达式将其删除 - How to search specific attribute within a tag and remove it using javascript regex 使用正则表达式或其他方式在 javascript 中找到彼此相邻的两个特定大写字母 - Using regex, or something else, to find two specific upper case letters right next to eachother in javascript <a href>如果标签具有特定格式,请在 javascript 中使用正则表达式删除标签</a> - Remove <a href > tag if it has specific format using regex in javascript 如何将2个字母的特定组合与正则表达式匹配 - how to match specific combination of 2 letters with regex 使用JavaScript Regex将字母替换为Words - Replace letters with Words using JavaScript Regex Javascript Regex-如何收集日期的前2个字母 - Javascript Regex - How to collect first 2 letters of date Javascript正则表达式:匹配任何系列的字母,除非它们匹配特定的序列 - Javascript Regex: Match any series of letters, unless they match a specific sequence 如何使用正则表达式删除字符串中所有出现的重复字母。 在 JS 中 - How to remove all occurrences of repeated letters in a string, using regex. In JS 如何仅在此特定脚本中验证字母-javascript - How to validate for letters only in this specific script - javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM