操作大同小异,但有一些特殊符号需要特殊处理,并且考虑只替换html元素<aa>...</aa>
中间的部分。
本文列几种不同的实现方法,自己研究吧。
- 字符串拆成数组循环方式之一
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;
}
- 字符串拆成数组循环方式之二
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;
}
- 字符串拆成数组循环与正则替换结合
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;
}
- 正则替换之一
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, '?');
});
}
- 正则替换之二
考虑&#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 || ';';
});
});
}