如何实现中英文排版优化

 let str = `"这是我们Docs团队的JavaScript总结\\nTypeScript是非常棒的语言"`;
console.log(str)
let cursorIndex = 0;
function isCn(str) {
    var reg = new RegExp("[\\\\u4E00-\\\\u9FFF]+", "g");
    if (reg.test(str)) {
        return true;
    }
}
function isEn(str) {
    if (/[A-Za-z]+/g.test(str)) {
        return true;
    }
}

function insertStr(source, start, newStr) {
    return source.slice(0, start) + newStr + source.slice(start);
}
while (cursorIndex < str.length) {
    let prevStr = str.charAt(cursorIndex);
    let curStr = str.charAt(cursorIndex + 1);

    if (isEn(prevStr) && isCn(curStr)) {
        str = insertStr(str, cursorIndex + 1, ' ');
        cursorIndex = cursorIndex + 2;
    } else if (isCn(prevStr) && isEn(curStr)) {
        str = insertStr(str, cursorIndex + 1, ' ');
        cursorIndex = cursorIndex + 2;
    } else {
        cursorIndex++;
    }
}

console.log(`-------- 中英文排版优化后 ---------`)
console.log(str)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

泰瑞_

知识源于创作热情,感谢你的支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值