js 读取word和txt(react版) + 正则分割段落

2021-03-29 10:26

阅读:574

标签:button   out   lse   -o   mon   reducer   split   ADG   文件   

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 (
    
); }; 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版) + 正则分割段落

标签:button   out   lse   -o   mon   reducer   split   ADG   文件   

原文地址:https://www.cnblogs.com/mapleChain/p/12609131.html

上一篇:JSON

下一篇:.net identity scaffold


评论


亲,登录后才可以留言!