正则匹配 & ' " < >

function(str) {
str
= dr.ConvertToString(str);
while (/&/.test(str)) {
str
= str.replace(/&/, "&amp;");
}
while (/'/.test(str)) {
str
= str.replace(/'/, "&apos;");
}
while (/"/.test(str)) {
str
= str.replace(/"/, "&quot;");
}

while (/</.test(str)) {
str
= str.replace(/</, "&lt;");
}
while (/>/.test(str)) {
str
= str.replace(/>/, "&gt;");
}

return str;
}

怎么改比较好?这样写会让人觉得正则基础太差.

慕田峪4524236
浏览 541回答 2
2回答

海绵宝宝撒

看实际需求 普陀的替换字符 就这样用吧
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript