常规replace使用
const text1 = 'abcdefg' const text2 = text1.replace('bc','00') // text2 = 'a00defg'
字典表匹配替换
const replacements = { '<': '<', '>': '>', '≥': '≥', '≤': '≤', '≠': '≠' } const text = '25<78 (27+9)≥36' const text2 = text.replace(/&(.+?);/g, function($0) { return replacements[$0] }) // text2 = '25<78 (27+9)≥36'
去除a标签
const text = '1.<a class=\"keyword\" href=\"【考点提示】\" style=\"pointer-events: none;text-decoration:none;color:#505F65\">【考点提示】</a>内容xxxx' // 保留标签里面的内容 const text2 = text.replace(/<a[^>]*>|<\/a>/g, '') // text2 = '1.【考点提示】内容xxxx' // 不保留标签内容 const text3 = text.replace(/<a[^>]*>(.+?)<\/a>/g, '') // text3 ='1.内容xxxx'
标签:const,text,js,text2,xxxx,replace,字典 From: https://www.cnblogs.com/xiaomaibu/p/17611857.html