>  Q&A  >  본문

사전 정의된 태그를 자동으로 교체하는 방법

<p>위와 같이 바꿔야 할 문자가 몇 개 있는데 어떻게 해야 할지 모르겠습니다. </p><p> 대체할 문자: </p> <pre class="brush:php;toolbar:false;">첫 번째 | <일> <빨간색> | </> |</pre> <p><code>day => 현재 날짜를 가져옵니다(예: 14)</code></p><p> <code>빨간색 => 색상 빨간색</p><p> <코드><a ###https://www.google.com/>링크</> <a href="https://www.google.com/"> ;/></code></p> <p><code>Enter: 안녕하세요<red>시로 씨</red></p><p> <code>출력: 안녕하세요<span style="color: red">Mr. Siro</span></code></p> <p>내 채팅 기록입니다. </p> <p>위 태그의 교체를 확인하는 일반 함수를 작성하는 방법을 알려주실 수 있나요? 내 코드는 다음과 같습니다. </p> <p> <pre class="snippet-code-js lang-js Prettyprint-override"><code>export const formatTags = (content) => const firstTag = "<빨간색>"; const secondTag = "</red>"; const TagsIndex = [...content.matchAll(new RegExp(firstTag, "gi"))].map( (a) => ); const 초기컨텐트 = 컨텐츠; TagsIndex.forEach((index) => { const tagContent = 초기Content.substring( 인덱스 + firstTag.length, initialContent.indexOf(secondTag, 인덱스) ); if (firstTag === "<빨간색>") { 콘텐츠 = 콘텐츠.replaceAll( `${firstTag}${tagContent}${secondTag}`, `<span style="color: red">${tagContent || "わなない"}</span>` ); } }); 콘텐츠 반환; };</code></pre> </p> <p> <pre class="snippet-code-html lang-html Prettyprint-override"><code><span :class="(msg.image || msg.file) && msg.text ? 'mt-2' : ''" v-html="msg.text" v-linkified:옵션="{ className: currentUserId === msg.senderId ? '메시지 외부 링크' : '', }" //></code></pre> </p> <p>제 영어 실력이 좋지 않아서 죄송합니다! </p><p> 다들 감사 해요! </p>
P粉330232096P粉330232096388일 전462

모든 응답(1)나는 대답할 것이다

  • P粉388945432

    P粉3889454322023-09-04 09:34:29

    대체 규칙을 만들 수 있는 Map。用于捕获文本的正则表达式将是键,replace대체 함수 콜백이 값이 됩니다. 규칙을 반복하고 문자열을 업데이트합니다.

    으아악 으아악

    회신하다
    0
  • 취소회신하다