js 读取word和txt(react版) + 正则分割段落
Posted mapleChain
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了js 读取word和txt(react版) + 正则分割段落相关的知识,希望对你有一定的参考价值。
show the code
前提:需要mammoth包~
import React, { useState, useReducer } from ‘react‘;
import { Button, Alert, Table, Badge, Input, Upload } from ‘antd‘;
import CommonTable from ‘@cps/CommonTable‘;
import styles from ‘./receive.less‘;
import { UploadOutlined } from ‘@ant-design/icons‘;
import mammoth from ‘mammoth‘;
function reducer(state: any, action: any) {
return {
...state,
...action.data
};
}
const NovelAnalyze = () => {
const [state, dispatch] = useReducer(reducer, {
content: ‘‘
});
const getTextInfo = (file: any) => {
const reader = new FileReader();
reader.readAsText(file, ‘gb2312‘);
reader.onload = (result: any) => {
console.log(result);
let targetNum = result.target.result;
console.log(targetNum);
};
return false;
};
const getWordInfo = (file: any) => {
const reader = new FileReader();
reader.onload = function(loadEvent: any) {
const arrayBuffer = loadEvent.target[‘result‘];
mammoth
.extractRawText({ arrayBuffer: arrayBuffer })
.then(function(resultObject) {
console.log(‘extractRawText‘, resultObject.value);
})
.done();
};
reader.readAsArrayBuffer(file);
return false;
};
return (
<div>
<ul className={styles.optionBtnList}>
<li>
<Button type=‘primary‘>导出</Button>
<Upload action=‘‘ accept=‘text/plain‘ beforeUpload={getTextInfo} showUploadList={false}>
<Button>
<UploadOutlined />
上传txt文件
</Button>
</Upload>
<Upload
action=‘‘
accept=‘.doc,.docx,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document‘
beforeUpload={getWordInfo}
showUploadList={false}
>
<Button>
<UploadOutlined />
上传word文件
</Button>
</Upload>
<Button type=‘primary‘>导入word</Button>
</li>
</ul>
</div>
);
};
export default NovelAnalyze;
正则分割段落
原文:
第259章 这是259内容 第262章 这是262内容 第666章 测试内容
str.replace(/s*(第d+章)s*/g, "@@@$1___").split("@@@").filter(item => item).map(item => ({[item.split("___")[0]]: item.split("___")[1]}))
结果:
0: {第259章: "这是259内容"}
1: {第262章: "这是262内容"}
2: {第666章: "测试内容"}
以上是关于js 读取word和txt(react版) + 正则分割段落的主要内容,如果未能解决你的问题,请参考以下文章