How can I modify the title of a site using JavaScript?

烈酒焚心 提交于 2019-12-02 08:47:50

You could use a regular expression, perhaps:

document.title = document.title.match(/"([^"]+)"/)[1]

This regex uses grouping to matches the first thing that is between quotes, and assigns it to the title (without the quotes).

Using a substr method is by far the simplest if you know that the beginning of the title will always be identical. For example, with the title you gave:

document.title = document.title.substr(76);

If it is not a static title, then RegEx would be the next most logical route. To match the last set of quotes and ignore potential whitespace:

document.title = document.title.match(/"([^"]+)"\s*$/)[1];

工具导航Map