天天看點

javascript學習筆記 - 英文标點符号替換成中文标點符号

操作大同小異,但有一些特殊符号需要特殊處理,并且考慮隻替換html元素

<aa>...</aa>

中間的部分。

本文列幾種不同的實作方法,自己研究吧。

  1. 字元串拆成數組循環方式之一
function changeEnglishPunctuation(html) {
    let symbols = {
      ":": ":",
      ".": "。",
      ",": ",",
      "!": "!",
      "?": "?",
      ";": ";"
    };
    
    let stack = [];
    let res = "";
    let inTag = false;
    
    for (let i = 0; i < html.length; i++) {
        if (html[i] === "<") {
            inTag = true;
            stack.push("<");
            res += "<";
            continue;
        }
        if (html[i] === ">") {
            inTag = false;
            stack.pop();
            res += ">";
            continue;
        }
        if (!inTag && symbols.hasOwnProperty(html[i])) {
            res += symbols[html[i]];
        } else {
            res += html[i];
        }
    }

    return res;
  }
           
  1. 字元串拆成數組循環方式之二
function changeEnglishPunctuation(html) {
    let inTag = false;
    let result = '';

    for (let i = 0; i < html.length; i++) {
      if (html[i] === '<') {
        inTag = true;
      } else if (html[i] === '>') {
        inTag = false;
      }

      if (!inTag) {
        switch (html[i]) {
          case ':':
            result += ':';
            break;
          case ';':
            result += ';';
            break;
          case '(':
            result += '(';
            break;
          case ')':
            result += ')';
            break;
          case ',':
            result += ',';
            break;
          case '.':
            result += '。';
            break;
          case '!':
            result += '!';
            break;
          case '?':
            result += '?';
            break;
          default:
            result += html[i];
        }
      } else {
        result += html[i];
      }
    }

    return result;
  }
           
  1. 字元串拆成數組循環與正則替換結合
function changeEnglishPunctuation(html) {
    const symbolMap = {
      ':': ':',
      ';': ';',
      '(': '(',
      ')': ')',
      ',': ',',
      '.': '。',
      '!': '!',
      '?': '?',
    };
    
    const replaceFn = (match) => {
      let result = '';
      for (const char of match) {
        result += symbolMap[char] || char;
      }
      return result;
    };
    
    const result = html.replace(/(?<=^|>)([^><]+?)(?=<|$)(?!&\S+;)/g, replaceFn);

    return result;
  }
           
  1. 正則替換之一
function changeEnglishPunctuation(html) {
    var r = /(?<=^|>)[^><]+?(?=<|$)/g;
    return html.replace(r, function (text) {
        return text.replace(/:/g, ':')
                   .replace(/;/g, ';')
                   .replace(/\(/g, '(')
                   .replace(/\)/g, ')')
                   .replace(/,/g, ',')
                   .replace(/\./g, '。')
                   .replace(/!/g, '!')
                   .replace(/\?/g, '?');
    });
  }
           
  1. 正則替換之二

    考慮

    &#qqbb;

    這種類型中的

    ;

    需要單獨寫(研究了三天的成果~~就那麼幾個字,累)
function changeAllEnglishPunctuation(html) {
    return html.replace(/(?<=^|>)[^><]+?(?=<|$)/g, function (text) {
      return text.replace(/\:/g, ':')
                  .replace(/\(/g, '(')
                  .replace(/\)/g, ')')
                  .replace(/,/g, ',')
                  .replace(/\./g, '。')
                  .replace(/\!/g, '!')
                  .replace(/\?/g, '?')
                  .replace(/(&#?[^;]+;)|(;)/g, (match, entity, semicolon) => {
                    return entity || ';';
                  });
    });
}
           

繼續閱讀