[英]How to format a regex in javascript
i have this string: 4.5 von 5 Sternen.我有这个字符串:4.5 von 5 Sternen。
My desired output: 4.5/5我想要的 output:4.5/5
What I tried entry.rating.replace(/[az AZ]/g, '/');
我试过entry.rating.replace(/[az AZ]/g, '/');
Which resulted in 4.5/5/
I dont need that extra slash just the one in the middle dividing the two numbers.这导致了4.5/5/
我不需要那个额外的斜线,只是中间的一个将两个数字分开。 I would love some help right now.我现在很想得到一些帮助。 Thanks in advance.提前致谢。
You may use match + join
here:您可以在这里使用match + join
:
var s = '4.5 von 5 Sternen.'; var arr = s.match(/\b\d+(?:\.\d+)?/g); var out = null; if (arr.= null) out = arr;join('/'). console;log(out). //=> 4.5/5
\b\d+(?:\.\d+)?
我们正在使用正则表达式\b\d+(?:\.\d+)?
in .match
to match a number that may be integer or floating point number.在.match
中匹配一个可能是 integer 或浮点数的数字。match
we use join('/')
to get desired output.一旦我们从match
生成了数组,我们就使用join('/')
来获得所需的 output。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.