简体   繁体   English

JavaScript正则表达式用百分号替换所有数字

[英]JavaScript regex to replace all numbers with percentage sign

I have to remove all numbers with percentage sign in string. 我必须删除所有带有百分号的数字。

For instance, 例如,

str = "test 12% test" -> "test test"
str = "test 12 % test" -> "test test"
str = "test 12%   test" -> "test test"
str = "test 1.2% test" -> "test test"

For now I cannot figure out how to fix the last one - it is number with dot. 目前,我不知道如何解决最后一个问题-它是带点的数字。 My current regexp is like this: 我当前的正则表达式是这样的:

name = name.replace(/\d+ ?% ?/g, "");

ADDITIONAL SCENARIOS 其他场景

str = "test 1.2%,test" -> "test ,test" // space is optional after %
str = "test (1.2%) test" -> "test test" // space is optional after %
str = "test 1.2%" -> "test" or "test " // space could be left after test if easier

You can add an optional non capturing group (?:\\.\\d+)? 您可以添加一个可选的非捕获组(?:\\.\\d+)? which checks for a dot and one or more digits. 检查一个点和一个或多个数字。 At the end your could add * to match zero or more whitespaces. 最后,您可以添加*以匹配零个或多个空格。

Explanation 说明

  • Match optional parenthesis \\(? 匹配可选的括号\\(?
  • Match one or more digits \\d+ 匹配一个或多个数字\\d+
  • Optional non capturing group to match a dot and one or more digits (?:\\.\\d+)? 可选的非捕获组,以匹配点和一个或多个数字(?:\\.\\d+)?
  • Match an optional space ? 匹配可选空间?
  • Match a percentage sign % 匹配百分号%
  • Match an optional closing parenthises \\) 匹配可选的右括号\\)
  • Match zero or more whitespaces * 匹配零个或多个空格*

\\(?\\d+(?:\\.\\d+)? ?%\\)? *

 var strings = [ "test 12% test", "test 12 % test", "test 12% test", "test 1.2% test", "test 1.2%,test", "test (1.2%) test", "test 1.2%" ]; for (var i = 0; i < strings.length; i++) { console.log(strings[i] + "-> " + strings[i].replace(/\\(?\\d+(?:\\.\\d+)? ?%\\)? */g, "")); } 

You could use the [] square brackets to specify a group to match. 您可以使用[]方括号指定要匹配的组。

here we are using 在这里我们正在使用

  • \\d : digit \\d :数字
  • \\. : literal . :文字.
  • \\s : whitespace \\s :空格

 const strings = [ "test 12% test", "test 12 % test", "test 12% test", "test 1.2% test", "test whitespace", "test 12 test", "test 1.2%,test", "test (1.2%) test", "test 1.2%", ] strings.forEach( string => console.log(string, string.replace(/\\s\\(?[\\d\\.\\s]+\\%\\)?\\s*/g, ' ').trim()) ) 
 <script src="https://codepen.io/synthet1c/pen/KyQQmL.js"></script> 

This will get your output 这将得到您的输出

Find [^\\S\\r\\n]*(?:\\d+(?:\\.\\d*)?|\\.\\d+)\\s*%[^\\S\\r\\n]* 找到[^\\S\\r\\n]*(?:\\d+(?:\\.\\d*)?|\\.\\d+)\\s*%[^\\S\\r\\n]*
Replace 更换

https://regex101.com/r/EcbAXV/1 https://regex101.com/r/EcbAXV/1

Formatted 格式化的

 [^\S\r\n]*                    # Trim horizontal whitespace
 (?:                           # Valid number formats to find
      \d+ 
      (?: \. \d* )?
   |  \. \d+ 
 )
 \s*                           # Optional whitespace
 %                             # Literal percent
 [^\S\r\n]*                    # Trim horizontal whitespace

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM