How can I replace a <
with an anchor as an HTML wrapper?
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:
<
with <a href="twitter.com/{{data.author}}">
>
with </a>
<a href="twitter.com/jgallardo949">jgallardo949</a>
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?
The top two work, the last one does not
<div class="label">With Profits Financial Strength:</div>
<div class="data rating">****</div>
<div class="data2 thing">+</div>
<div class="author twitter"> > </div>
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;
A >
in HTML gets returned as >
so doing like this (\\>|>)
and it will find both.
var n2=str3.replace(/(\>|>)/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(/(\\>|>)/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>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.