js 读取word和txt(react版) + 正则分割段落
2021-03-29 10:26
标签:button out lse -o mon reducer split ADG 文件 前提:需要mammoth包~ 原文: 结果: js 读取word和txt(react版) + 正则分割段落 标签:button out lse -o mon reducer split ADG 文件 原文地址:https://www.cnblogs.com/mapleChain/p/12609131.htmlshow the code
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 (
正则分割段落
第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章: "测试内容"}
上一篇:JSON
文章标题:js 读取word和txt(react版) + 正则分割段落
文章链接:http://soscw.com/index.php/essay/69477.html