1

i'm trying whole the time to replace such strings:

<title id="hello">my title </title>
<title >my title </title>
<title id="hello" class="blue">my title </title>

i need regex, which replace text between title-tags, undepend attributes. sadly i get only second example with this regex:

str.replace(/<\/?title>/g,'')

Has anybody ideas?

Roma Kap
  • 517
  • 1
  • 8
  • 23

1 Answers1

1

It's always better to avoid using regex for parsing HTML.

RegEx match open tags except XHTML self-contained tags

Using regular expressions to parse HTML: why not?


Instead, generate a temporary DOM element with the content and applying all the change finally get the HTML content.

var html = `<title id="hello">my title </title>
<title >my title </title>
<title id="hello" class="blue">my title </title>`;

// generate a temporary div elementt
var temp = document.createElement('div');
// set its html content as the string
temp.innerHTML = html;

//do the rest here
// get all title tags
Array.from(temp.getElementsByTagName('title'))
  // iterate over the title tag and do the necessary chenges
  .forEach(function(ele) {
    ele.innerHTML = 'new content'
  })

// get back the updated html content from dom element
console.log(temp.innerHTML);

Fore NodeJS refer : HTML-parser on Node.js

Community
  • 1
  • 1
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
  • Thats nice solution, but in my context it´s does not help me. I investigate web-sites for title-tag. I get a body from any web-page and look for title-tag. Some web-pages have within title-tag attributes and some pages not. I dont have "document"-Object. I am working with NodeJs. – Roma Kap Jan 12 '17 at 18:55