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版) + 正则分割段落的主要内容,如果未能解决你的问题,请参考以下文章

c语言中如何读取一个文件word.txt(10M),并把读取的文件写进磁盘中的word1.txt.求代码急急。。。

JS/前端/H5:实现txt文件上传并读取其中内容

java文件读取(word,txt)

C# 读取txt文件生成Word文档

Python批量读取加密Word文档转存txt文本实现

C语言程序,想从本地读取“word.txt”中的内容,并逐行显示在显示器上,怎么写?