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章: "测试内容"}
    
  • 相关阅读:
    ubuntu 下安装memcache 以及php扩展
    js控制页面显示和表单提交
    phpcms--使用添加php原生支持
    phpcms v9 升级视频云问题推荐位不能添加
    phpcms—— 内容中的附件调用和添加远程地址的调用
    phpcms--模型管理,推荐位管理,类别管理
    linux shell 编程
    css中的定位和框模型问题
    php生成静态文件
    打印机问题win7 和xp
  • 原文地址:https://www.cnblogs.com/mapleChain/p/12609131.html
Copyright © 2011-2022 走看看