• 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章: "测试内容"}
    
  • 相关阅读:
    hibernate案例 测试代码
    android开发 单击按钮 实现页面间的跳转
    hibernate的dao操作不能提交到数据库问题的解决
    hibernate初探
    Could not find action or result 导致 页面出现404错误
    严重: Exception starting filter struts2
    myeclipse 右键 Add Struts... 页面报404 错误
    tomcat错误信息解决方案【严重:StandardServer.await: create[8005]
    struts2 package元素配置(转载)
    TensorFlow和深度学习新手教程(TensorFlow and deep learning without a PhD)
  • 原文地址:https://www.cnblogs.com/mapleChain/p/12609131.html
Copyright © 2020-2023  润新知