Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
604 views
in Technique[技术] by (71.8m points)

javascript - Regex to replace all superscript numbers

I'm struggling to figure out a reasonable solution to this. I need to replace the following characters: ?123?????? using a regex replace. I would think that you would just do this:

item = item.replace(/[?123??????]/g, '');

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here is the simple regex for finding all superscript numbers

/p{No}/gu/

Breakdown:

  • 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

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

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 Solution

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

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].

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...