1

Question

How can I replace a < with an anchor as an HTML wrapper?

Background

I am getting a JSON value with a Twitter user's name as something like <jgallardo949>

Since i don't want that printed to the page:

  • i want to replace the < with <a href="twitter.com/{{data.author}}">
  • and the > with </a>
  • The end result in the code will be <a href="twitter.com/jgallardo949">jgallardo949</a>
  • The end result on the page will just be: jgallardo949

I referenced other similar questions that I was able to find here and elsewhere. I got a start with the answers on Replace string of text javascript

My followup practice worked. But specifically the > symbol is having a challenge, or i am missing something?

Code

The top two work, the last one does not

HTML

<div class="label">With Profits Financial Strength:</div>
<div class="data rating">****</div>
<div class="data2 thing">+</div>
<div class="author twitter"> > </div>

JS

var str=document.getElementsByClassName("data" ,"raiting")[0].innerHTML; 
var n=str.replace(/\*/g,"star");
document.getElementsByClassName("data", "raiting")[0].innerHTML=n;



var str2=document.getElementsByClassName("data2" ,"thing")[0].innerHTML; 
var n2=str2.replace(/\+/g,"<h1>moon</h1>");
document.getElementsByClassName("data2", "thing")[0].innerHTML=n2;


var str3=document.getElementsByClassName("author" ,"twitter")[0].innerHTML; 
var n2=str3.replace(/\>/g,"<h1>moon3</h1>");
document.getElementsByClassName("author", "twitter")[0].innerHTML=n2;

enter image description here

2
  • Is the Twitter username always prefixed with exactly 1 < and suffixed with exactly 1 >? Or is this username an exception? Commented Jul 5, 2018 at 21:22
  • Yes @Peter, the username would be displayed in the format i listed Commented Jul 5, 2018 at 21:40

1 Answer 1

3

A > in HTML gets returned as &gt; so doing like this (\>|&gt;) and it will find both.

var n2=str3.replace(/(\>|&gt;)/g,"<h1>moon3</h1>");

Stack snippet

var str=document.getElementsByClassName("data" ,"raiting")[0].innerHTML; 
var n=str.replace(/\*/g,"star");
document.getElementsByClassName("data", "raiting")[0].innerHTML=n;



var str2=document.getElementsByClassName("data2" ,"thing")[0].innerHTML; 
var n2=str2.replace(/\+/g,"<h1>moon</h1>");
document.getElementsByClassName("data2", "thing")[0].innerHTML=n2;


var str3=document.getElementsByClassName("author" ,"twitter")[0].innerHTML; 
var n2=str3.replace(/(\>|&gt;)/g,"<h1>moon3</h1>");
document.getElementsByClassName("author", "twitter")[0].innerHTML=n2;
<div class="label">With Profits Financial Strength:</div>
<div class="data rating">****</div>
<div class="data2 thing">+</div>
<div class="author twitter"> > </div>

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

9 Comments

This does absolutely answer my question, thank you. The problem is that I did not ask the right question since I also need to escape some values and perhaps not do innerHTML.
@JGallardo Based on how you will inject, not using innerHTML might inject a HTML element like this: jsfiddle.net/by0z3ujn ... and what about escape some values, what you mean to do with that?
Also wondering about targeting the character inside of a string. By itself, the > is wrapped just fine. But when something like <name> is returned, i am getting moon3 moon3 so looks like the middle portion (name) is not displayed at all, and either left or right (less-than greater-than) is subject to the change.
@JGallardo If you make me a jsfiddle, similar to my answer code sample, showing that <name> issue, I'll have a look.
Thanks, in the example, i am now just trying to do a simple wrap with an <h1> jsfiddle.net/L6ay70sp
|

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.