0

I am using the replace method as follows:

<code v-html="'/<test>/'.replace(/(?:\r\n|\r|\n)/g, 'testing')"></code>

I have a larger string that I am testing against with many \n but I noticed with the above replace function to remove these characters, somehow the value <test> inside // gets replaced.

1 Answer 1

2

The /<test>/ string isn't turning into //, it's being rendered by the v-html directive. To more accurately test your replace method I'd recommend using the v-text directive instead.

Below is a demonstration of the difference in output between the two directives.

new Vue({
  el:"#app"
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <p>Using v-html</p>
  <code v-html="'/<test>/'.replace(/(?:\r\n|\r|\n)/g, 'testing')"></code>
  <br/>
  <p>Using v-text</p>
  <code v-text="'/<test>/'.replace(/(?:\r\n|\r|\n)/g, 'testing')"></code>
</div>

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

3 Comments

Actually, I think what's happening is that html is trying to render <test>, so that is why it is blank. The regex isn't actually doing anything there, but rather html is trying to render this as a tag.
But how do you render the new lines then, where previously I was replacing them.
I'm unsure of what your goal is. Your original question is attempting to replace newline characters with the string 'testing' Are you trying to replace newline characters with a line break <br/> instead?

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.