替换字符串Javascript中的上标和下标字符 [英] Replace superscript and subscript chars from a string Javascript

查看:139
本文介绍了替换字符串Javascript中的上标和下标字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从文本中删除所有上标和下标字符.

I want to remove all superscript and subscript chars from the text.

Exp:'⁰'.

我在stackoverflow上找到了示例,但它仅考虑上标数字,而不考虑字符或下标.

I found an example on stackoverflow, but it only considers superscript numbers and not characters or subscripts.

有人知道如何实现这一目标吗?一种方法是拥有所有可能的上标和下标,并一一替换,但这有点不切实际.

Anyone knows how to achieve this? A way would be to have all possible superscripts and subscripts and replace them one by one but that is a bit impractical.

推荐答案

基于

Based on the subscript and superscript Unicode range reference and a manual search for "subscript" and "superscript" in a UniView tool, you may use

.replace(/[\u006E\u00B0\u00B2\u00B3\u00B9\u02AF\u0670\u0711\u2121\u213B\u2207\u29B5\uFC5B-\uFC5D\uFC63\uFC90\uFCD9\u2070\u2071\u2074-\u208E\u2090-\u209C\u0345\u0656\u17D2\u1D62-\u1D6A\u2A27\u2C7C]+/g, '')

请参见 regex演示.

+ 量词(一个或多个连续出现)将使正则表达式引擎更轻松地一次性删除1+个子/上标字符的整个块.

The + quantifier (one or more consecutive occurrences) will make it easier for the regex engine to remove whole chunks of 1+ sub/superscript chars in one go.

请注意,ᵀᴹ修饰语字母,而不是正式的上标字符.如果要包含它们,则需要

Note that ᵀᴹ are modifier letters and are not formally superscript chars. If you want to include them, you need

var res = s.replace(/(?:\uD81A[\uDF40-\uDF43]|\uD81B[\uDF93-\uDF9F\uDFE0]|[\u006E\u00B0\u00B2\u00B3\u00B9\u02AF\u0670\u0711\u2121\u213B\u2207\u29B5\uFC5B-\uFC5D\uFC63\uFC90\uFCD9\u2070\u2071\u2074-\u208E\u2090-\u209C\u0345\u0656\u17D2\u1D62-\u1D6A\u2A27\u2C7C\u02B0-\u02C1\u02C6-\u02D1\u02E0-\u02E4\u02EC\u02EE\u0374\u037A\u0559\u0640\u06E5\u06E6\u07F4\u07F5\u07FA\u081A\u0824\u0828\u0971\u0E46\u0EC6\u10FC\u17D7\u1843\u1AA7\u1C78-\u1C7D\u1D2C-\u1D6A\u1D78\u1D9B-\u1DBF\u2071\u207F\u2090-\u209C\u2C7C\u2C7D\u2D6F\u2E2F\u3005\u3031-\u3035\u303B\u309D\u309E\u30FC-\u30FE\uA015\uA4F8-\uA4FD\uA60C\uA67F\uA69C\uA69D\uA717-\uA71F\uA770\uA788\uA7F8\uA7F9\uA9CF\uA9E6\uAA70\uAADD\uAAF3\uAAF4\uAB5C-\uAB5F\uFF70\uFF9E\uFF9F])+/g, '')

请参见此演示

要规范下标和上标数字,使用字典并在作为替换参数传递的匿名方法中动态替换是有意义的:

To normalize subscript and superscript digits, it makes sense to use a dictionary and replace dynamically within an anonymous method passed as the replacement argument:

var super_sub_script_dict = {'\u2070': '0', '\u00B9': '1', '\u00B2': '2', '\u00B3': '3', '\u2074': '4', '\u2075': '5', '\u2076': '6', '\u2077': '7', '\u2078': '8', '\u2079': '9', '\u2080': '0', '\u2081': '1', '\u2082': '2', '\u2083': '3', '\u2084': '4', '\u2085': '5', '\u2086': '6', '\u2087': '7', '\u2088': '8', '\u2089': '9'};
var test_string = "Subscript: ₀₁₂₃₄₅₆₇₈₉ and superscript: ⁰¹²³⁴⁵⁶⁷⁸⁹";
var regex = new RegExp('[' + Object.keys(super_sub_script_dict).join("") + ']', 'g'); // => /[⁰¹²³⁴⁵⁶⁷⁸⁹₀₁₂₃₄₅₆₇₈₉]/g
// Or
// var regex = /[\u00B9\u00B2\u00B3\u2070\u2074-\u2089]/g;
console.log(test_string.replace(regex, function(x) { 
    return super_sub_script_dict[x];
}))

这篇关于替换字符串Javascript中的上标和下标字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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