如何转换:NIFTY 16th JAN 12300 CE
进入NIFTY 16<sup>th</sup> JAN 12300 CE
使用 jQuery?
最佳答案
要实现此目的,您可以使用正则表达式。为了帮助消除当目标字符串出现在单词内时出现误报的可能性,您可以让正则表达式专门针对 st
、nd
进行查找、rd
或 th
字符串(当它们跟随长度为 1 或 2 个字符的整数时)。试试这个:
["NIFTY 16th JAN 12300 CE", "rd ND 21st April"].forEach(v => {
let output = v.replace(/(\d{1,2})(st|nd|rd|th)/gi, '$1<sup>$2</sup>');
console.log(output);
});
关于javascript - 如何使用jquery从数字中分割后缀?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59719279/