国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

數(shù)據(jù)截取處理、富文本去除所有標(biāo)簽

這篇具有很好參考價(jià)值的文章主要介紹了數(shù)據(jù)截取處理、富文本去除所有標(biāo)簽。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

前提:在做項(xiàng)目中有個(gè)需求是填寫(xiě)表單后生成一份文檔,文檔可以編輯、保存。

這部分用富文本處理了,涉及到的邏輯就是對(duì)象-->富文本標(biāo)簽形式

在給后端傳的數(shù)據(jù)格式再把富文本標(biāo)簽形式-->對(duì)象形式。

涉及到文字,圖片、表格,以及圖片表格的標(biāo)題。

數(shù)據(jù)截取處理

        // 數(shù)據(jù)截取處理
        extractString(str, startChar, endChar) {
            const pattern = `${startChar}(.*?)${endChar}`;
            const regex = new RegExp(pattern);
            const matches = str.match(regex);
            return matches ? matches[1] : '';
        },

用法:

let a = this.extractString(item, '<img', '</p>')

?item就是要處理的字串,后面一個(gè)開(kāi)始元素,一個(gè)結(jié)束元素。

富文本去除所有標(biāo)簽

str='<div class="proposal-content" style="text-indent:2em; font-size:16px; line-height:1.5">光伏組件在運(yùn)行過(guò)程中,光電轉(zhuǎn)換效率會(huì)受到影響,輸出功率有所降低。本報(bào)告年衰減率根據(jù)《光伏制造行業(yè)規(guī)范條件(2021年本)》計(jì)算,晶硅組件衰減率首年不高于2.5%,后續(xù)每年不高于0.6%。</div>'
let a = str.replace(/<[^>]+>/g, '')
// 得到的結(jié)果就是去除所有標(biāo)簽的:光伏組件在運(yùn)行過(guò)程中,光電轉(zhuǎn)換效率會(huì)受到影響,輸出功率有所降低。本報(bào)告年衰減率根據(jù)《光伏制造行業(yè)規(guī)范條件(2021年本)》計(jì)算,晶硅組件衰減率首年不高于2.5%,后續(xù)每年不高于0.6%。

.replace替換其他數(shù)據(jù):

proposalString = proposalString.replace(new RegExp('<p data-we-empty-p="" style="padding-left:2em;">', 'g'), paragraph);

‘g’是全局替換,不然只會(huì)替換第一個(gè)。

富文本轉(zhuǎn)對(duì)象大致實(shí)現(xiàn)思路:

因?yàn)橐獢?shù)據(jù)對(duì)應(yīng),以下是后端傳過(guò)來(lái)的數(shù)據(jù):

          let obj = [
                {
                    content:
                        '合浦位于廣西壯族自治區(qū)北海市合浦縣合浦閘口鎮(zhèn)。項(xiàng)目所在地的經(jīng)緯度坐標(biāo)為21.69°N, 109.52°E。\n合浦縣,屬亞熱帶季風(fēng)型海洋性氣候區(qū),日照較強(qiáng),熱量充足,雨量充沛,夏熱冬暖,無(wú)霜期長(zhǎng)。氣候受季風(fēng)環(huán)流控制,雨熱同季。冬干夏濕,夏無(wú)酷暑,冬無(wú)嚴(yán)寒,盛行風(fēng)向有明顯的季節(jié)性轉(zhuǎn)換,在沿海鄉(xiāng)鎮(zhèn)還有晝夜交替的海陸風(fēng)出現(xiàn)。由于各季節(jié)雨熱不均以及瀕臨北部灣,主要?dú)庀鬄?zāi)害有臺(tái)風(fēng)、暴雨、干旱、低溫陰雨及霜凍、冰雹、雷電和龍卷風(fēng)等。主要?dú)夂蛱卣魇悄昶骄鶜鉁仄?,年總降雨量偏多,年總?cè)照諘r(shí)數(shù)偏多。\n',
                    image: {
                        title: '項(xiàng)目所在位置圖',
                        path: 'https://xidianai.oss-cn-hangzhou.aliyuncs.com/xdai_78abb1eb_loc_map.png',
                        index: 1,
                    },
                    table: {
                        title: '',
                        rowNum: '',
                        colNum: '',
                        head: [],
                        content: [],
                    },
                },
            ];

對(duì)應(yīng)回顯在富文本,

思路:遍歷數(shù)據(jù)結(jié)構(gòu),加上標(biāo)簽,富文本本身是會(huì)回顯標(biāo)簽的

                         this.$nextTick(() => {
                            let content = '';
                            this.chapterList.chapterContent.forEach(item => {
                                if (item.content) {
                                    content += item.content
                                        .replace(/\n\n\n/g, '<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;')
                                        .replace(/\n\n/g, '<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
                                    content = content.replace(/\n/g, `<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`);
                                }
                                // 圖片回顯
                                if (item.image.path) {
                                    content += `<div class="img" style="width: 100%;display: flex;flex-direction: column;justify-content: center;align-items: center;"><img src=${item.image.path} /><div style="font-size: 14px;">圖${this.chapterList.chapterIndex}-${item.image.index}  <span class="img-title">${item.image.title}</span></div></div><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`;
                                }
                                // 表格回顯
                                if (item.table.title) {
                                    content += `<div class="table" style="width:100%;display: flex;flex-direction: column;justify-content: center;align-items: center;"><span>表${this.chapterList.chapterIndex}-${item.table.index}  ${item.table.title}</span><table border="1" cellspacing="0" cellpadding="0" style="width: 100%;"><tbody><tr>`;
                                    item.table.head.forEach(item => {
                                        content += `<th>${item}</th>`;
                                    });
                                    content += `<tr>`;
                                    item.table.content.forEach(item => {
                                        content += `<tr>`;
                                        item.forEach(item => {
                                            content += `<td>${item}</td>`;
                                        });
                                        content += `</tr>`;
                                    });
                                    content += `</tbody></table></div><div class="proposal-content" style="text-indent:2em; font-size:16px; line-height:1.5"></div>`;
                                }
                            });
                            content = `<div class="proposal-content" style="text-indent:2em; font-size:16px; line-height:1.5">${content
                                .replace(`<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`, ``)
                                .replace(new RegExp('°E。'), `°E。<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`)}</div>`;
                            this.editor.txt.html(content);
                        });

用戶編輯完成后保存,富文本轉(zhuǎn)對(duì)象操作思路:特定標(biāo)簽用replace()替換后,split()成數(shù)組,這樣obj就拿到了,然后遍歷,分開(kāi)處理content文字,圖片,以及圖片標(biāo)題,表格以及表格標(biāo)題。

        // 保存
        saveProposal() {
            console.log(this.editor.txt.html());
            let obj = [
                {
                    content:
                        '合浦位于廣西壯族自治區(qū)北海市合浦縣合浦閘口鎮(zhèn)。項(xiàng)目所在地的經(jīng)緯度坐標(biāo)為21.69°N, 109.52°E。\n合浦縣,屬亞熱帶季風(fēng)型海洋性氣候區(qū),日照較強(qiáng),熱量充足,雨量充沛,夏熱冬暖,無(wú)霜期長(zhǎng)。氣候受季風(fēng)環(huán)流控制,雨熱同季。冬干夏濕,夏無(wú)酷暑,冬無(wú)嚴(yán)寒,盛行風(fēng)向有明顯的季節(jié)性轉(zhuǎn)換,在沿海鄉(xiāng)鎮(zhèn)還有晝夜交替的海陸風(fēng)出現(xiàn)。由于各季節(jié)雨熱不均以及瀕臨北部灣,主要?dú)庀鬄?zāi)害有臺(tái)風(fēng)、暴雨、干旱、低溫陰雨及霜凍、冰雹、雷電和龍卷風(fēng)等。主要?dú)夂蛱卣魇悄昶骄鶜鉁仄?,年總降雨量偏多,年總?cè)照諘r(shí)數(shù)偏多。\n',
                    image: {
                        title: '項(xiàng)目所在位置圖',
                        path: 'https://xidianai.oss-cn-hangzhou.aliyuncs.com/xdai_78abb1eb_loc_map.png',
                        index: 1,
                    },
                    table: {
                        title: '',
                        rowNum: '',
                        colNum: '',
                        head: [],
                        content: [],
                    },
                },
            ];
            // 段落
            let paragraph = '<div class="proposal-content" style="text-indent:2em; font-size:16px; line-height:1.5">';
            let proposalArr = [];
            // let proposalString =
            //     '<div class="proposal-content" style="text-indent:2em; font-size:16px; line-height:1.5">光伏組件在運(yùn)行過(guò)程中,光電轉(zhuǎn)換效率會(huì)受到影響,輸出功率有所降低。本報(bào)告年衰減率根據(jù)《光伏制造行業(yè)規(guī)范條件(2021年本)》計(jì)算,晶硅組件衰減率首年不高于2.5%,后續(xù)每年不高于0.6%,25年內(nèi)不。本項(xiàng)目運(yùn)營(yíng)期內(nèi)逐年上網(wǎng)電量見(jiàn)下表。<div class="table" style="width:100%;display: flex;flex-direction: column;justify-content: center;align-items: center;"><span>表6-1  本項(xiàng)目運(yùn)營(yíng)期逐年上網(wǎng)電量統(tǒng)計(jì)表</span></div><table border="0" width="100%" cellpadding="0" cellspacing="0"><tbody><tr><th>年份</th><th>年發(fā)電量萬(wàn)kWh</th><th>累計(jì)發(fā)電量萬(wàn)kWh</th><th>年利用小時(shí)數(shù)h</th></tr><tr></tr><tr><td>1</td><td>59525.59</td><td>59525.59</td><td>1026.3</td></tr><tr><td>19</td><td>55196.46</td><td>1089859.46</td><td>951.66</td></tr></tbody></table><p><br/></p>&nbsp; &nbsp; &nbsp; &nbsp;</div>';
            let proposalString = this.editor.txt.html();
            // text-align:left; text-align:center;替換(圖后文本)
            proposalString = proposalString.replace(
                new RegExp(
                    '<div class="proposal-content" style="text-indent:2em; font-size:16px; line-height:1.5; text-align:center;">',
                    'g'
                ),
                paragraph
            );
            proposalString = proposalString.replace(
                new RegExp(
                    '<div class="proposal-content" style="text-indent:2em; font-size:16px; line-height:1.5; text-align:left;">',
                    'g'
                ),
                paragraph
            );
            // 新增文本
            proposalString = proposalString.replace(new RegExp('<p data-we-empty-p="" style="padding-left:2em;">', 'g'), paragraph);
            proposalString = proposalString.replace(new RegExp(paragraph), ``).replace(new RegExp(paragraph, 'g'), `aabb`).split('aabb');
            proposalString.forEach(item => {
                let content = '';
                let image = {};
                let table = {};
                // 處理圖片數(shù)據(jù)
                if (this.extractString(item, 'src="', '"').length > 0) {
                    image.path = this.extractString(item, 'src="', '"');
                    if (item.includes('<span class="img-title">')) {
                        // 圖片標(biāo)題
                        image.title = this.extractString(item, '<span class="img-title">', '</span>');
                        item = item.replace(this.extractString(item, '<img', '</span>'), '');
                    } else {
                        // 編輯圖片標(biāo)題(居中)
                        image.title = this.extractString(item, '<p data-we-empty-p="" style="text-align:center;">', ' ');
                        image.title = this.extractString(item, image.title, '</p>');
                        // 去除img文字
                        item = item.replace(this.extractString(item, '<img', '</p>'), '');
                    }
                } else {
                    image.path = '';
                    image.title = '';
                }
                // 處理表格數(shù)據(jù)
                if (item.indexOf('tbody><tr><th>') > -1) {
                    let tableData = this.extractString(item, '<table', '</table>');
                    let tableHeadArr = []; // 截取的要處理的數(shù)據(jù)
                    let head = []; // 表頭
                    let contentList = []; // 表格內(nèi)容
                    // 獲取表頭
                    if (item.indexOf('<th>') > -1) {
                        tableHeadArr = tableData.replace(new RegExp('<th>', 'g'), 'tableHead').split('tableHead');
                    }
                    tableHeadArr.forEach(m => {
                        if (m.indexOf('</th>') > -1) {
                            head.push(this.extractString(m, '', '</th>'));
                        }
                    });
                    // 獲取表格內(nèi)容
                    let tableContentOld = tableData.split('</th></tr>')[1]
                    if (tableContentOld.indexOf('<tr><td>') > -1) {
                        tableContentOld = tableContentOld.replace(new RegExp('<tr><td>', 'g'), 'tableContent').split('tableContent');
                        tableContentOld.forEach(m => {
                          let contentArr = []
                          if(m.replace(/<[^>]+>/g, '')){
                            m.replace(new RegExp('</td>', 'g'),'cloumn').replace(new RegExp('<td>', 'g'),'').split('cloumn').forEach((n)=>{
                              if(n.replace(/<[^>]+>/g, '')){
                                contentArr.push(n);
                              }
                            })
                            contentList.push(contentArr);
                          }
                        })
                        // 去除table文字
                        item = item.replace(this.extractString(item, '<img', '</p>'), '');
                    }
                    // table.title = title;
                    table.head = head
                    table.content = contentList
                    table.colNum = head.length
                    table.rowNum = contentList.length
                }
                // 段落文字處理(去除圖片、表格數(shù)據(jù)、標(biāo)簽、占位符等)
                content = item.replace(new RegExp('<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;', 'g'), '\n').replace(/<[^>]+>/g, '');
                proposalArr.push({ content: content, image: image, table: table });
            });
            console.log(proposalArr);
        },

?代碼貼在這里,存在冗余,大致提供一個(gè)思路,因?yàn)樾枨筮€沒(méi)評(píng)審,暫時(shí)把這部分功能做出來(lái)了。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-813349.html

到了這里,關(guān)于數(shù)據(jù)截取處理、富文本去除所有標(biāo)簽的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 【前端web入門(mén)第一天】01 開(kāi)發(fā)環(huán)境、HTML基本語(yǔ)法文本標(biāo)簽

    【前端web入門(mén)第一天】01 開(kāi)發(fā)環(huán)境、HTML基本語(yǔ)法文本標(biāo)簽

    文章目錄: 1. 準(zhǔn)備開(kāi)發(fā)環(huán)境 1.1 vs Code基本使用 2.HTML文本標(biāo)簽 2.1 標(biāo)簽語(yǔ)法 2.2 HTML基本骨架 2.3 標(biāo)簽的關(guān)系 2.4 注釋 2.5 標(biāo)題標(biāo)簽 2.6 段落標(biāo)簽 2.7 換行與水平線標(biāo)簽 2.8 文本格式化標(biāo)簽 VSCode與谷歌瀏覽器離線版,安裝包評(píng)論區(qū)自提. VSCode默認(rèn)安裝位置:C:UsershpAppDataLocalProgramsMic

    2024年01月25日
    瀏覽(52)
  • web前端之多行文本擦除效果、文本逐個(gè)顯示或展示、創(chuàng)建元素標(biāo)簽、querySelector、createElement、appendChild、requestAnimationFrame

    前言 window.requestAnimationFrame()告訴瀏覽器——你希望執(zhí)行一個(gè)動(dòng)畫(huà),并且要求瀏覽器在下次重繪之前調(diào)用指定的回調(diào)函數(shù)更新動(dòng)畫(huà)。該方法需要傳入一個(gè)回調(diào)函數(shù)作為參數(shù),該回調(diào)函數(shù)會(huì)在瀏覽器下一次重繪之前執(zhí)行。 備注:若你想在瀏覽器下次重繪之前繼續(xù)更新下一幀動(dòng)畫(huà)

    2024年03月24日
    瀏覽(27)
  • RSAUtil 前端 JavaScript JSEncrypt 實(shí)現(xiàn) RSA (長(zhǎng)文本)加密解密

    文章歸檔:https://www.yuque.com/u27599042/coding_star/cl4dl599pdmtllw1 import JSEncrypt from ‘jsencrypt’ import {stringIsNull} from “@/utils/string_utils.js”:https://www.yuque.com/u27599042/coding_star/slncupw7un3ce7cb import {isNumber} from “@/utils/number_utils.js”:https://www.yuque.com/u27599042/coding_star/tuwmm3ghf5lgo4bw 注意: 此方

    2024年04月22日
    瀏覽(31)
  • 前端 富文本編輯器原理——從javascript、html、css開(kāi)始入門(mén)

    前端 富文本編輯器原理——從javascript、html、css開(kāi)始入門(mén)

    大家好,我是yma16,本文分享關(guān)于前端 富文本編輯器原理——從javascript、html、css開(kāi)始。 富文本編輯器 富文本編輯器是指具有格式化文本和圖像編輯功能的文本編輯器 參考文檔:https://w3c.github.io/selection-api/#abstract 全局屬性 contenteditable 是一個(gè)枚舉屬性,表示元素是否可被用

    2024年02月08日
    瀏覽(29)
  • 【數(shù)據(jù)動(dòng)態(tài)填充到element表格;將帶有標(biāo)簽的數(shù)據(jù)展示為文本格式】

    【數(shù)據(jù)動(dòng)態(tài)填充到element表格;將帶有標(biāo)簽的數(shù)據(jù)展示為文本格式】

    一:數(shù)據(jù)動(dòng)態(tài)填充到element表格; 二:將帶有標(biāo)簽的數(shù)據(jù)展示為文本格式; 1、 2、data 3、methods

    2024年02月15日
    瀏覽(20)
  • js實(shí)現(xiàn)一行半文本的截取

    最近遇到一個(gè)需求是要在第二行的中間截取文本,因?yàn)樵诤竺娴觅N一個(gè)圖標(biāo),所以這種情況用常規(guī)的css截取文本有點(diǎn)難處理。于是在上網(wǎng)查閱后發(fā)現(xiàn)了幾個(gè)方法:第一種是用偽元素加定位,把.;11..蓋在文字的上面;第二種就是用js來(lái)實(shí)現(xiàn)了。 首先貼下常規(guī)的css截取文本的代

    2024年02月10日
    瀏覽(18)
  • chatgpt賦能python:Python數(shù)據(jù)處理之去除NaN值

    作為數(shù)據(jù)分析和處理領(lǐng)域中的一種高效工具,Python 在數(shù)據(jù)清理方面表現(xiàn)優(yōu)異。而 NaN 是數(shù)據(jù)處理中常見(jiàn)的問(wèn)題之一,過(guò)多的 NaN 值常常會(huì)導(dǎo)致分析結(jié)果不準(zhǔn)確或無(wú)法得出結(jié)論,因此 Python 提供了多種方法去除 NaN 值。 NaN 即 Not A Number 的縮寫(xiě),表示不是一個(gè)數(shù)字。NaN 值是在進(jìn)行

    2024年02月14日
    瀏覽(20)
  • 用Python+OpenCV截取視頻中所有含有字幕的畫(huà)面

    用Python+OpenCV截取視頻中所有含有字幕的畫(huà)面

    有的視頻文件的字幕已經(jīng)壓制到了 視頻的圖像中 ,不能單獨(dú)提取出字幕文件。網(wǎng)上的 “提取視頻字幕” 網(wǎng)站多為提取視頻中的字幕文件,而非識(shí)別視頻 圖像中 的字幕。少數(shù)通過(guò)OCR技術(shù)識(shí)別畫(huà)面中字幕的工具需要在線運(yùn)行、運(yùn)行速度較慢,或者需要收費(fèi),使用不夠靈活。

    2024年04月10日
    瀏覽(18)
  • js如何截取某個(gè)字符串前面所有的字符串

    利用substring()方法截取出字符 1.新建一個(gè)字符串 2.通過(guò)indexOf()方法獲取你想截止到的那個(gè)字符 3.通過(guò)substring()方法,從字符串0位置開(kāi)始截取至\\\"p\\\"前面的字符串

    2024年02月12日
    瀏覽(100)
  • 【前端異?!縅avaScript錯(cuò)誤處理:分析 Uncaught(in promise) error

    【前端異?!縅avaScript錯(cuò)誤處理:分析 Uncaught(in promise) error

    在開(kāi)發(fā)過(guò)程中,JavaScript的錯(cuò)誤處理是一個(gè)老生常談的話題。當(dāng)應(yīng)用程序發(fā)生未捕獲的異常時(shí),Uncaught(in promise) error是其中最常見(jiàn)的錯(cuò)誤類(lèi)型。這篇文章將從多個(gè)方面詳細(xì)闡述這種錯(cuò)誤類(lèi)型的原因與解決方案。 Promise是一種用于異步編程的原生JavaScript對(duì)象。它提供了一種處理異

    2024年02月05日
    瀏覽(103)

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包