[英]Javascript regex: replace letters near any number
I have a string:我有一个字符串:
F1 F#1 F2 F#2
I want to convert it to:我想将其转换为:
f1 F1 f2 F2
Numbers near f/F
can be 0
to 9
f/F
附近的数字可以是0
到9
Is it possible to do this with regex?用正则表达式可以做到这一点吗? Thanks you!
谢谢!
You can do this using back-references.您可以使用反向引用来做到这一点。 Putting parentheses around sections of your regular expression allows you to reference them in a return function (in order from left to right).
将括号放在正则表达式的部分周围允许您在返回函数中引用它们(按从左到右的顺序)。
The following code works like you specified, first by changing the instances without # to lower case, then using a second regex to replace the ones with #s.以下代码的工作方式与您指定的一样,首先将不带 # 的实例更改为小写,然后使用第二个正则表达式替换带有 #s 的实例。
a = "F1 F#1 F2 F#2";
b = a.replace(/([A-Za-z])([0-9])/g,function(match,$1,$2,original){return $1.toLowerCase() + $2;}).replace(/([A-Za-z])#([0-9])/g,"$1$2");
//b = f1 F1 f2 F2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.