正则表达式替换所有上标数字 [英] Regex to replace all superscript numbers

查看:137
本文介绍了正则表达式替换所有上标数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力寻找一个合理的解决方案.我需要替换以下字符:使用正则表达式替换.我认为您会这样做:

I'm struggling to figure out a reasonable solution to this. I need to replace the following characters: ⁰¹²³⁴⁵⁶⁷⁸⁹ using a regex replace. I would think that you would just do this:

item = item.replace(/[⁰¹²³⁴⁵⁶⁷⁸⁹]/g, '');

但是,当我尝试这样做时,notepad ++会将符号5-9转换为常规脚本编号.我意识到这可能与我使用的编码格式有关,我将其设置为ANSI.

However, when I try to do that, notepad++ converts symbols 5-9 into regular script numbers. I realize this probably relates to the encoding format I am using, which I see is set to ANSI.

我从来没有真正理解过各种编码格式之间的区别.但是我想知道是否有解决此问题的简便方法?

I've never really understood the difference between the various encoding formats. But I'm wondering if there is any easy fix for this issue?

推荐答案

这是用于查找所有上标数字的简单正则表达式

Here is the simple regex for finding all superscript numbers

/\p{No}/gu/

故障:

  • \p{No}匹配上标或下标数字,或者不是数字[0-9]的数字
  • u modifier:Unicode:模式字符串被视为UTF-16.还会导致转义序列匹配unicode字符
  • g modifier:全局.所有比赛(不会在第一场比赛中返回)
  • \p{No} matches a superscript or subscript digit, or a number that is not a digit [0-9]
  • u modifier: unicode: Pattern strings are treated as UTF-16. Also causes escape sequences to match unicode characters
  • g modifier: global. All matches (don't return on first match)

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

现在,大多数现代浏览器仍未在regex中内置对unicode码的支持.我建议使用xregexp

Now, most modern browsers still have no built in support for unicode numbers in regex. I would recommend using the xregexp library

XRegExp提供增强的(和可扩展的)JavaScript正则表达式.除了浏览器本身支持的功能之外,您还可以获得新的现代语法和标志. XRegExp还是regex实用程序带,其工具可简化您的客户端grep和解析,同时使您不必担心JavaScript regexe的烦人方面,例如跨浏览器不一致或手动操作lastIndex.

XRegExp provides augmented (and extensible) JavaScript regular expressions. You get new modern syntax and flags beyond what browsers support natively. XRegExp is also a regex utility belt with tools to make your client-side grepping and parsing easier, while freeing you from worrying about pesky aspects of JavaScript regexes like cross-browser inconsistencies or manually manipulating lastIndex.

http://xregexp.com/

HTML解决方案

HTML具有用于表示上标文本的<sup>标签.

HTML has a <sup> tag for representing superscript text.

标签定义上标文本.上标文字显示在法线上方半个字符,有时以较小的字体呈现.上标文本可以用作脚注,例如WWW [1].

The tag defines superscript text. Superscript text appears half a character above the normal line, and is sometimes rendered in a smaller font. Superscript text can be used for footnotes, like WWW[1].

如果有上标数字,则html标记几乎肯定具有sup标记.

If there are superscript numbers, the html markup almost surely has the sup tag.

var math = document.getElementById("math");

math.innerHTML = math.innerHTML.replace(/<sup>[\d]?<\/sup>/g, "");

<p id="math">4<sup>2</sup>+ 3<sup>2</sup></p>

这篇关于正则表达式替换所有上标数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆