0

I have created websites with reactjs. For restAPI's i used java and CMS is liferay. In liferay i had created the shortcode for accordion like

('[accordion][acc-header]Heading 1[/acc-header][acc-content]Content1[/acc-content][acc-header]Heading 2[/acc-header][acc-content]Content2[/acc-content][/accordion]').

I want to replace html elements instead of this strings. How to do in javascript?

Here you go,

[accordion]
 [acc-header]Heading 1[/acc-header][acc-content]Content1[/acc-content]
 [acc-header]Heading 2[/acc-header][acc-content]Content2[/acc-content]     
[/accordion]

Html output,

<div class="accordion">
  <h3>Heading1</h3>
  <div class="accordion-content">
    content 1
  </div>
<h3>Heading2</h3>
  <div class="accordion-content">
    content 2
  </div>
</div>
2
  • What is the desired HTML output? Commented Oct 10, 2016 at 12:19
  • @TomFenech, i have edited my question. can you please check now? Commented Oct 10, 2016 at 12:26

1 Answer 1

2

Dirty and quick way:

var s = '[accordion][acc-header]Heading 1[/acc-header][acc-content]Content1[/acc-content][acc-header]Heading 2[/acc-header][acc-content]Content2[/acc-content][/accordion]';
s.replace(/\[([a-z\-]+)]/g, '<div class="$1">').replace(/\[\/[a-z\-]*]/g, '</div>')

The result is:

"<div class="accordion"><div class="acc-header">Heading 1</div><div class="acc-content">Content1</div><div class="acc-header">Heading 2</div><div class="acc-content">Content2</div></div>"

This won't add <h3> tags

Sign up to request clarification or add additional context in comments.

1 Comment

That's unfair =) added an edit, but that's not a complete solve

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.