const tables = <root><datagrid id='table1' data='data' border=true font=宋体 fontSize=12…
- User
const tables =
<root><datagrid id='table1' data='data' border=true font=宋体 fontSize=12 thHeight=18 tdHeight=16> <column label='序号' prop='sn' width='50' thAlign=center></column> <column label='样本编号' prop='barCode' width='50'></column> <column label='样本类型' prop='specimenType' width='50'></column> <column label='样本量' prop='curCapacity' width='50'></column> <column label='有效日期' prop='storeEndDate' width='100'></column> <column label='样本状态' prop='status' width='50'></column> <column label='样本位置' prop='positionName' width='50'></column> </datagrid></root>转成xml文档 - Assistant
- User
<datagrid id='table1' data='data' border=true font=宋体 fontSize=12 thHeight=18 tdHeight=16>\n<column label='序号' prop='sn' width='50' thAlign=center></column>\n<column label='样本编号' prop='barCode' width='50'></column>\n<column label='样本类型' prop='specimenType' width='50'></column>\n<column label='样本量' prop='curCapacity' width='50'></column>\n<column label='有效日期' prop='storeEndDate' width='100'></column>\n<column label='样本状态' prop='status' width='50'></column>\n<column label='样本位置' prop='positionName' width='50'></column>\n</datagrid>将没有用引号包裹的属性值加上引号 - Assistant
- User
\n\n\n\n\n\n\n\n 用正则将没有用引号包裹的属性值加上引号
- Assistant
可以使用以下正则表达式将没有用引号包裹的属性值加上引号:
s/<([a-z]+)([^>]*)(\w+=\w+)([^>]*)>/$1$2"$3"$4>/ig其中:
<匹配起始标签<([a-z]+)匹配标签名,只允许小写字母([^>]*)匹配标签的其它属性(\w+=\w+)匹配没有用引号包裹的属性值,\w+匹配属性名和属性值([^>]*)匹配剩余的属性>匹配标签结束
替换为
$1$2"$3"$4>即可。示例代码:
const str = `<datagrid id='table1' data='data' border=true font=宋体 fontSize=12 thHeight=18 tdHeight=16>\n<column label='序号' prop='sn' width='50' thAlign=center></column>\n<column label='样本编号' prop='barCode' width='50'></column>\n<column label='样本类型' prop='specimenType' width='50'></column>\n<column label='样本量' prop='curCapacity' width='50'></column>\n<column label='有效日期' prop='storeEndDate' width='100'></column>\n<column label='样本状态' prop='status' width='50'></column>\n<column label='样本位置' prop='positionName' width='50'></column>\n</datagrid>`; const reg = /<([a-z]+)([^>]*)((\w+=\w+)([^>]*))>/ig; const result = str.replace(reg, "$1$2\"$3\""); console.log(result);