库X崔羊羊 2021-07-03 14:49 采纳率: 100%
浏览 68
已结题

请问,JS脚本中,怎么把引号里的括号当做字符串文本处理

代码如下,是我抄来的,作用是替换文本汉化网页
就是开头这个(RC)这行不生效,我试着在括号前面加一个反斜杠没成功,请问应该如何让它被替换成中文呢?
多谢各位大佬啦!!!QvQ

(function() {
    'use strict';

    const i18n = new Map([
        ['When this unit is placed on (RC) from hand', '这个单位从手牌登场到R时'],
        ['and put it into your drop if it isn\'t', '不是的话,放置到弃牌区'],
        ['Diabolos, "Violence" Bruce', '魔惧会 “暴虐” 布鲁斯'],
    ])

    replaceText(document.body)

    const bodyObserver = new MutationObserver(mutations => {
        mutations.forEach(mutation => {
            mutation.addedNodes.forEach(addedNode => replaceText(addedNode))
        })
    })
    bodyObserver.observe(document.body, { childList: true, subtree: true })

    function replaceText(node) {
        nodeForEach(node).forEach(htmlnode => {
            i18n.forEach((value, index) => {
                // includes可直接使用 === 以提高匹配精度
                const textReg = new RegExp(index, 'g')
                if (htmlnode instanceof Text && htmlnode.nodeValue.includes(index)) {
                    htmlnode.nodeValue = htmlnode.nodeValue.replace(textReg, value)}
                else if (htmlnode instanceof HTMLInputElement && htmlnode.value.includes(index)) {
                    htmlnode.value = htmlnode.value.replace(textReg, value)}
            })
        })
    }

    function nodeForEach(node) {
        const list = []
        if (node.childNodes.length === 0) list.push(node)
        else {
            node.childNodes.forEach(child => {
                if (child.childNodes.length === 0) list.push(child)
                else list.push(...nodeForEach(child))
            })
        }
        return list
    }
})();
  • 写回答

2条回答 默认 最新

  • CSDN专家-showbo 2021-07-03 15:49
    关注

    ()是正则的关键字,分组用的,所以需要转义才能匹配()字符。而不是作为分组。但是转义后,又和你的htmlnode.nodeValue.includes代码冲突了。因为内容变为了\(RC\),和原始内容(RC)不匹配,去掉includes就可以了。如下

    img

    解决了你的问题能点个采纳吗,谢谢~

    When this unit is placed on (RC) from hand
    and put it into your drop if it isn't
    Diabolos, "Violence" Bruce
    <script>
        (function () {
            'use strict';
            const i18n = new Map([////////这里注意转义(),需要2个\\,因为最后需要内容为\(,直接用\(没有这种转义字符,会变为(内容,又无效了
                ['When this unit is placed on \\(RC\\) from hand', '这个单位从手牌登场到R时'],
                ['and put it into your drop if it isn\'t', '不是的话,放置到弃牌区'],
                ['Diabolos, "Violence" Bruce', '魔惧会 “暴虐” 布鲁斯'],
            ])
            replaceText(document.body)
            const bodyObserver = new MutationObserver(mutations => {
                mutations.forEach(mutation => {
                    mutation.addedNodes.forEach(addedNode => replaceText(addedNode))
                })
            })
            bodyObserver.observe(document.body, { childList: true, subtree: true })
            function replaceText(node) {
                nodeForEach(node).forEach(htmlnode => {
                    i18n.forEach((value, index) => {
                        const textReg = new RegExp(index, 'g')
                        if (htmlnode instanceof Text ) {//去掉includes
                            htmlnode.nodeValue = htmlnode.nodeValue.replace(textReg, value)
                        }
                        else if (htmlnode instanceof HTMLInputElement ) {//去掉includes
                            htmlnode.value = htmlnode.value.replace(textReg, value)
                        }
                    })
                })
            }
            function nodeForEach(node) {
                const list = []
                if (node.childNodes.length === 0) list.push(node)
                else {
                    node.childNodes.forEach(child => {
                        if (child.childNodes.length === 0) list.push(child)
                        else list.push(...nodeForEach(child))
                    })
                }
                return list
            }
        })();
    
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 4月25日

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line