# Regexp - replacing htmlText with valid xhtml

**URL:** <https://forum.kirupa.com/t/regexp-replacing-htmltext-with-valid-xhtml/237633>\
**Category:** Uncategorized\
**Created:** [September 1, 2007, 12:42pm UTC](https://forum.kirupa.com/t/regexp-replacing-htmltext-with-valid-xhtml/237633 "2007-09-01T12:42:32Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![pete\_zahut](https://avatars.discourse-cdn.com/v4/letter/p/91b2a8/32.png) [@pete\_zahut](https://forum.kirupa.com/u/pete_zahut)\
**Post date:** [September 1, 2007, 12:42pm UTC](https://forum.kirupa.com/t/regexp-replacing-htmltext-with-valid-xhtml/237633/1 "2007-09-01T12:42:32Z")

</div>

Hi,

I’m building a custom RichTextEditor so the user can set a title, subtitle and paragraph in the text. But because the htmlText of the textArea returns depracated html, I wanted to replace it with valid xhtml.

So I have something like this:

```auto
<TEXTFORMAT LEADING="2"><P ALIGN="LEFT"><FONT FACE="Verdana" SIZE="20" COLOR="#232323" LETTERSPACING="0" KERNING="1">This should be a h1 title</FONT></P></TEXTFORMAT>

```

That should be replaced to:

```auto
<h1>This should be a h1 title</h1>

```

But the problem is that I can’t find a way to close the h1 with \</h1\> without doing it for both \</h1\> and \</h2\>

This is the code to replace the start-tags \<h1\>,\<h2\> and \<p\>:  
[AS]pattern = /\<P.\*\>\<FONT._SIZE=“20”._?\>/g;  
str = str.replace(pattern, “\<h1\>”);

pattern = /\<P.\*\>\<FONT._SIZE=“14”._?\>/g;  
str = str.replace(pattern, “\<h2\>”);

pattern = /\<P ALIGN=“LEFT”\>/g;  
str = str.replace(pattern, “\<p\>”);[/AS]

This code closes the tags:

[AS]pattern = /\</FONT\>\</P\>/g;  
str = str.replace(pattern, “\</h1\>”);

pattern = /\</P\>/g;  
str = str.replace(pattern, “\</p\>”);[/AS]

But in case I have a title and a subtitle it closes it twice with \</h1\>. Anyone an idea? I’m looking into the String.search() function but I’m not getting any results…

Thx!
