JavaScript error causing issue with 'keywordDensity' assessment
- [x] I've read and understood the contribution guidelines.
- [x] I've searched for any related issues and avoided creating a duplicate issue.
Please give us a description of what happened.
My focus keyphrase is depression and I have used the word multiple times in the article, yet I have an error message in the analysis results which says
An error occurred in the 'keywordDensity' assessment

The console in Google Chrome reported that there is an invalid regular expression used in the javascript.

SyntaxError: Invalid regular expression: /depression')/: Unmatched ')'
at String.search (<anonymous>)
at e.default (wp-seo-analysis-worker-1121.min.js:17)
at wp-seo-analysis-worker-1121.min.js:17
at String.replace (<anonymous>)
at d (wp-seo-analysis-worker-1121.min.js:17)
at wp-seo-analysis-worker-1121.min.js:17
at Array.forEach (<anonymous>)
at e.markWordsInSentences (wp-seo-analysis-worker-1121.min.js:17)
at Object.e.default [as keywordCount] (wp-seo-analysis-worker-1121.min.js:21)
at W.getResearch (wp-seo-analysis-worker-1121.min.js:17)
n @ wp-seo-analysis-worker-1121.min.js:21
Technical info
- WordPress version: 5.2–en_GB
- Yoast SEO version: 11.2.1
- If relevant, which editor is affected (or editors):
- [ ] Classic Editor
- [x] Gutenberg
- [ ] Classic Editor plugin
- Which browser is affected (or browsers):
- [ ] IE11
- [ ] Edge
- [x] Chrome
- [x] Firefox
- [ ] Safari
Hi @rogersopen, would you mind sharing your text with us so that we can test this locally? Have you also tried to verify that this isn't a plugin conflict, just to be sure?
Yes, @Djennez I checked for plugin conflicts before posting this using the Health Check & Troubleshooting plugin. The text it affects is published so you can freely view it at https://www.whatstheproblem.co.uk/articles/psychology/depression-is-not-just-low-mood/
I was unable to replicate this issue with the text you've provided. Some things you can try:
- If you remove any instances of the word
depression)- note the bracket on the end - from the text, does this fix the issue? - The same for any instances of the word
'depression', this time the instances with the single quotes around. - Removing any 2 level-deep indentations (so in lists, remove any second level text), does that fix the issue?
That's interesting @Djennez - The offending phrase was 'depression') within a long quote, yet removing the single quote marks will stop me quoting as written in the source page. I tried escaping these single quote marks using ' with no success, as WordPress seems to replace them with single quote characters, even when the block is an HTML block.
Bingo! Thanks for your help @Djennez - I have found a solution where I can use ‘ and ’ instead. These are proper single quote marks anyway.
Whilst semantically it is incorrect to do so, most people do use apostrophes as single quote marks due to the fact that the correct character is not available on a standard computer keyboard. Because Gutenberg is supposed to help those who know little to no HTML, I feel this is a serious flaw to a degree. Therefore, I am going to raise a support ticket with the WordPress developers to correct this issue via suitable methods.
@Djennez As I mentioned at https://github.com/WordPress/gutenberg/issues/15816#issuecomment-495918867, I wonder if this is not something where Yoast SEO might be able to do a better job at escaping its regex. The error message seems to be pretty clear about that.
@swissspidy @rogersopen thanks for the reminder, I had this issue on my list to investigate a bit further. Seems that any keyphrase in the content appended with ') will trigger this error. I was unable to find the regex responsible for this problem.
I will put this on the backlog for our development team to have a look at.