Reputation: 556
My String : "<p>Stacked"
I want to replace it to "<font color=' blue'><</font>p<font color='blue'>></font>Stacked
So I have used this : ⬇
var rox = /([<])([a-z1-9]+)([>])/;
function testInfo(phoneInput){
var startTag = "<font color='blue'>";
var endTag = "</font>";
var OK = phoneInput.value.replace(rox, function(x){
var lt = x.replace(/([<])/, startTag+'<'+endTag);
var gt = lt.replace(/([>])/ , startTag+'>'+endTag);
return gt;
});
It returns like this
<font color='blue'<font color='blue'>p<font color='blue'>></font>
It means when I am replacing the Stack
**<font color='blue'><</font>p<font color='blue'>></font>** But next time it is replacing the **">"** of the *font tag*. Lot *">"* of **p tag**.
Problem
How can I fix it? I am trying something to color/highlight code.
Upvotes: 0
Views: 119
Reputation: 10466
You may try this:
str = `<p>Stacked`;
const subst1 = `LT_LTfont color=' blue'GT_GT<`;
const subst2 = `LT_LTfont color=' blue'GT_GT>`;
str = str.replace(/</g, subst1);
str = str.replace(/>/g, subst2);
str = str.replace(/GT_GT/g,`>`);
str = str.replace(/LT_LT/g,`<`);
$("#kk").html(str);
console.log(str);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="kk">
</div>
Upvotes: 1