6

I have a string in which there are continous occurances of a font tag

 <font color="blue">DATA ENTRY</font> 

and in some cases like this

 <font class="beat">DATA ENTRY</font> 

I want to replace the 2 tags with

So that it looks like this

     <p>DATA ENTRY</p>

I tried this ,can anyone please suggest me help.Thanks.

 text = text.replace('<font [^"]*>',<p>).replace('</font>','');
7
  • The <font> is a deprecated tag. Is there a reason why you need to use it? You could instead use the same <p> tag and just remove the class later. Commented Jul 12, 2017 at 10:52
  • Hi Nikhil,I am geeting this data from client side which I cannot manipulate. Commented Jul 12, 2017 at 10:55
  • Don't use regex with HTML. Instead convert it to a temporary DOM Element, manipulate it, and then convert back to text if needed. Commented Jul 12, 2017 at 11:01
  • @Nicer getting this data from client side means? you have to convert in server side (javascript in nodejs) or client side (javascript in browser)? Commented Jul 12, 2017 at 11:04
  • @Nicer angular & javascript you are using are the client side. Do you mean you can't touch the HTML ? Commented Jul 12, 2017 at 11:10

7 Answers 7

9
block.outerHTML = "<p>" + block.innerHTML + "</p>" 

where block is any HTML block it just left to select it correctly with:

var block = document.querySelector(".selector");

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

1 Comment

Didn't think about outerHTML being writeable. Good to know it is. Thanks :-)
4

If you want to stick with your simple string manipulation, you need to use regular expressions and correct the replacements in your replace calls:

text = text.replace(/<font[^>]*>/g,'<p>').replace(/<\/font>/g,'</p>');

Comments

3

Since you just need to replace the string you can do this with just one replace statement.

text = text.replace(/<(\/*)font[^>]*>/g, '<$1p>');

Comments

1

If you using jQuery with replaceWith

$('font').replaceWith('<p>DATA ENTRY</p>');

1 Comment

OP is using Angular js.So,I think using jquery might not be an good option.
0

First of all the font tag is deprecated and should not be used.

  1. Get an array of the tags you want to replace.

    var elems = document.getElementsByTagName('font');
    
  2. Go through loop and replace old HTML with new HTML

    for (var i = 0; i < elems.length; i++)
    {
        var target = elems[i].innerHTML;
        elems[i].innerHTML = target.replace(/(<p)/igm, '<font').replace(/<\/p>/igm, '</font>');
    }
    

Note: This is not tested but should work.

Comments

0

Try like this :

$('font').contents().unwrap().wrap('<p/>');

In javascript, you can do something like this :

var str="<font>hello world</font>";   
str = str.replace(/<font>/, "<p>");  
str = str.replace(/<\/font>/,"</p>"); 

1 Comment

OP is using Angular js.So,I think using jquery might not be an good option.
0

You may use the DOM API, which is specifically built to manipulate HTML safely — unlike regex, which can easily break on nested or malformed HTML.

One such tool is DOMParser, which lets us turn a string of HTML into actual DOM elements that we can work with as if they were part of the page.

function replaceFontWithP(htmlString) {
  const parser = new DOMParser();
  const doc = parser.parseFromString(htmlString, 'text/html');

  doc.querySelectorAll('font').forEach(font => {
    const p = document.createElement('p');
    p.innerHTML = font.innerHTML;
    font.replaceWith(p);
  });

  return doc.body.innerHTML;
}

const inputHTML = `
  <font color="blue">DATA ENTRY</font>
  <font class="beat"><b>Bold Text</b></font>
`;

const outputHTML = replaceFontWithP(inputHTML);
console.log(outputHTML);

Comments

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.