Delete string after replace

I use tinymce to implement wysiwyg editor and I try to find multiple code block by regular expression,replace backtics and toggle code block.But when I try to replace backticks and if I have more then one string of content before code block,all my strings before code block immidiately squizes to one.

For example if I have this content:

enter image description here

after replacing backticks and toggling code block I have this content:

enter image description here

so my second string content just go to the first string and second string disapper.

This is my code:

'use client';
import React, {FC, useEffect, useRef, useState} from 'react';
import {Editor} from '@tinymce/tinymce-react';
import styles from './editor.module.scss';
import Image from 'next/image';
import Arrow from '@assets/svg/arrow_right.svg';
import Link from 'next/link';
import {addImage, handleClick, readFile, saveFile} from '@/app/editor/Functions';
import {EditorEvent} from 'tinymce';


const ArticleEditor: FC = () => {
        const editorRef = useRef<any>(null);
        const [editorState, setEditorState] = useState('');
        const [htmlValue, setHtmlValue] = useState('');
        const blockStyles: {
            [key: number]: { symbol: string; tag: string; }
        } = {
            0 : {symbol : '[', tag : 'a'},
            1 : {symbol : '```', tag : 'pre'},
        };
        let newRegex: RegExp;
        const startOffset = editorRef.current ? editorRef.current.selection.getRng().startOffset : 0;
        const endOffset = editorRef.current ? editorRef.current.selection.getRng().endOffset : 0;
        
        const [keyboardEvent, setKeyboardEvent] =
            useState<EditorEvent<KeyboardEvent>>();
        
        
        // function to build regex
        const wrapRegex = (symbol: string) => {
            if (symbol === '```') {
                newRegex = /^```n+(?<text>((w+)?([sS]*?)))n+```$/gm;
            } else if (symbol === '[') {
                newRegex = /[(?<text>.*)]((?<link>.*))/gm;
            }
            if (newRegex) {
                const matchRegex = newRegex.exec(editorState);
                if (matchRegex && matchRegex.groups) {
                    console.log(matchRegex);
                    for (const key in blockStyles) {
                        const value = blockStyles[key];
                        if (symbol === value.symbol) {
                            replaceStyleSymbols(symbol, value.tag, matchRegex);
                        }
                    }
                }
                
            }
            
        };
 
        
        
        const replaceStyleSymbols = (symbol: string, tag: string, regexGroups?: RegExpExecArray) => {
            const {text} = getFullWordWithCoordinates(symbol, keyboardEvent ? keyboardEvent : null);
            if (regexGroups && regexGroups.groups) {
               if (symbol === '```') {
                    const content = editorRef.current.getContent({format : 'text'});
                    if (keyboardEvent && keyboardEvent.key === 'Enter') {
                        const codeText = regexGroups.groups.text.replaceAll('nn', 'n');
                        const newContent = editorState.replace(text,
                            `<p><pre><code>${codeText}</code></pre></p>`);
                        editorRef.current.setContent(newContent);
                        editorRef.current.focus();
                        editorRef.current.selection.select(editorRef.current.getBody(), true);
                        editorRef.current.selection.collapse(false);
                    }
                    
                }
                
        };
        const getFullWordWithCoordinates = (symbol: string, event: EditorEvent<KeyboardEvent> | null) => {
            let wholeWordStart = symbol === '```' ? editorState.indexOf(symbol) : startOffset;
            let wholeWordEnd = symbol === '```' ? editorState.length : endOffset;
            if (event && event.key === ' ') {
                wholeWordStart--;
            }
            
            if (symbol === '```') {
                while (editorState.charAt(wholeWordEnd) !== symbol && wholeWordEnd < editorState.length) {
                    wholeWordEnd++;
                }
            } 
            return {
                text : editorState.slice(wholeWordStart,
                    symbol === '[' ? editorState.indexOf(')') + 1 : wholeWordEnd),
                start : wholeWordStart,
                end : wholeWordEnd
            };
        };
        
        // function to handle matched symbol with dictionary value
        const handleRegex = () => {
            
            let currentValue: { symbol: string; tag: string } = {symbol : '', tag : ''};
            for (const key in blockStyles) {
                const value = blockStyles[key];
                if (editorState.includes(value.symbol)) {
                    currentValue = value;
                }
            }
            if (currentValue) {
              
                    wrapRegex(currentValue.symbol);
                
            }
        };
        
      
        useEffect(() => {
            handleRegex();
        }, [editorState]);
        
        
        return (
            <>
                <div className={styles.article__editor}>
                    
                    
                    <Editor>
                        onInit={(_evt, editor) => (editorRef.current = editor)}
                        inline={true}
                        onClick={(event) => handleClick(event, editorRef)}
                        onEditorChange={(newValue, editor) => {
                            setHtmlValue(newValue);
                            setEditorState(editor.getContent({format : 'text'}));
                        }}
                       
                        init={{
                            height : 500,
                            menubar : false,
                            plugins : [
                                'advlist',
                                'lists',
                                'importcss',
                                'image',
                                'autolink',
                                'preview',
                                'visualblocks',
                                'code',
                                'codesample',
                                'fullscreen',
                                'media',
                                'table',
                            ],
                           }}
            </>
        );
    }
;

export default ArticleEditor;

Can someone tell me how resolve my problem?

I will be appreciate for any help.

I tried to add

tag after replacing code block and tried to change object for replace,for example I tried to replace form editorRef.content,but it still doesn’t work.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật