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?

1
  • 1
    What are you trying to do? Commented Jan 12, 2017 at 18:07

1 Answer 1

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

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

1 Comment

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.

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.