zoukankan      html  css  js  c++  java
  • 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章: "测试内容"}
    
  • 相关阅读:
    Redpine的Lite-Fi解决方案获Wi-Fi CERTIFIED认证
    植物园偶遇一直喵
    美食篇
    端午节路过南站
    黄山云海
    一品黄山 天高云淡
    黄山的日出日落
    宏村,寻找你的前世今生
    git把本地文件上传到github上的步骤
    一张照片一个故事
  • 原文地址:https://www.cnblogs.com/mapleChain/p/12609131.html
Copyright © 2011-2022 走看看