Javascript 如何将正则表达式用于通用数字?

Javascript 如何将正则表达式用于通用数字?,javascript,regex,Javascript,Regex,对于replace方法,我有以下正则表达式规则: svg = svg.replace(/NS1:data:longestSeries=\"(.*?)\"/g, ''); svg = svg.replace(/NS2:data:longestSeries=\"(.*?)\"/g, ''); svg = svg.replace(/NS3:data:longestSeries=\"(.*?)\"/g, ''); svg = svg.replace(/NS4:data:longestSeries=\"(

对于
replace
方法,我有以下正则表达式规则:

svg = svg.replace(/NS1:data:longestSeries=\"(.*?)\"/g, '');
svg = svg.replace(/NS2:data:longestSeries=\"(.*?)\"/g, '');
svg = svg.replace(/NS3:data:longestSeries=\"(.*?)\"/g, '');
svg = svg.replace(/NS4:data:longestSeries=\"(.*?)\"/g, '');
svg = svg.replace(/NS5:data:longestSeries=\"(.*?)\"/g, '');

如何创建一个包含上述所有内容的规则?

您可以对所有允许的数字使用字符集:

/NS[1-5]:data:longestSeries=".*?"/g
或者,如果这是针对每个数字的,只需使用
\d
说明符:

/NS\d:data:longestSeries=".*?"/g
正如@Utkanos所提到的,您不需要逃避双引号,因为它们在正则表达式模式中没有任何特殊意义


最后,您不需要在
*?
周围加括号,因为您没有将捕获的组用于任何事情。

您可以对所有允许的数字使用字符集:

/NS[1-5]:data:longestSeries=".*?"/g
或者,如果这是针对每个数字的,只需使用
\d
说明符:

/NS\d:data:longestSeries=".*?"/g
正如@Utkanos所提到的,您不需要逃避双引号,因为它们在正则表达式模式中没有任何特殊意义


最后,您不需要在
*?
周围加括号,因为您没有将捕获的组用于任何事情。

还需要指出的是,没有必要转义引号。如果它不是连续的数字,(?:1 | 2 | 3 | 4 | 5),但是对于给定的问题,[1-5]解决方案很漂亮。还值得指出的是,没有必要转义引号。如果不是连续的数字,(?:1 | 2 | 3 | 4 | 5),但对于给定的问题,[1-5]解决方案很漂亮。