diff --git a/packages/yoastseo/spec/languageProcessing/helpers/word/markWordsInSentenceSpec.js b/packages/yoastseo/spec/languageProcessing/helpers/word/markWordsInSentenceSpec.js
index 1fc5049810e..0347abefee7 100644
--- a/packages/yoastseo/spec/languageProcessing/helpers/word/markWordsInSentenceSpec.js
+++ b/packages/yoastseo/spec/languageProcessing/helpers/word/markWordsInSentenceSpec.js
@@ -168,4 +168,34 @@ describe( "test the deconstructAnchor and reconstructAnchor helper", () => {
const reconstructedAnchor = reConstructAnchor( deconstructedAnchor.openTag, deconstructedAnchor.content );
expect( reconstructedAnchor ).toEqual( testAnchor );
} );
+
+ it( "correctly deconstructs and reconstructs an anchor if does not contain content", () => {
+ // Unrealistic Scenario. But protects against the bug that is solved in this PR:
+ // https://github.com/Yoast/wordpress-seo/pull/19373
+ const testAnchor = "";
+ const deconstructedAnchor = deConstructAnchor( testAnchor );
+
+ expect( deconstructedAnchor ).toEqual( {
+ openTag: "",
+ content: "",
+ } );
+
+ const reconstructedAnchor = reConstructAnchor( deconstructedAnchor.openTag, deconstructedAnchor.content );
+ expect( reconstructedAnchor ).toEqual( testAnchor );
+ } );
+
+ it( "correctly deconstructs and reconstructs an anchor if content contains a newline", () => {
+ // Unrealistic Scenario. But protects against the bug that is solved in this PR:
+ // https://github.com/Yoast/wordpress-seo/pull/19373
+ const testAnchor = "This is a line.\nAnd this is a line.";
+ const deconstructedAnchor = deConstructAnchor( testAnchor );
+
+ expect( deconstructedAnchor ).toEqual( {
+ openTag: "",
+ content: "This is a line.\nAnd this is a line.",
+ } );
+
+ const reconstructedAnchor = reConstructAnchor( deconstructedAnchor.openTag, deconstructedAnchor.content );
+ expect( reconstructedAnchor ).toEqual( testAnchor );
+ } );
} );
diff --git a/packages/yoastseo/src/languageProcessing/helpers/word/markWordsInSentences.js b/packages/yoastseo/src/languageProcessing/helpers/word/markWordsInSentences.js
index b6252dd1264..e428f8dee97 100644
--- a/packages/yoastseo/src/languageProcessing/helpers/word/markWordsInSentences.js
+++ b/packages/yoastseo/src/languageProcessing/helpers/word/markWordsInSentences.js
@@ -6,7 +6,8 @@ import matchWords from "../match/matchTextWithArray";
import arrayToRegex from "../regex/createRegexFromArray";
// Regex to deconstruct an anchor into open tag, content and close tag.
-const anchorDeconstructionRegex = /(]+>)(.+?)(<\/a>)/;
+// [^] matches any character, including newline
+const anchorDeconstructionRegex = /(]+>)([^]*?)(<\/a>)/;
/**
* Deconstructs an anchor to the opening tag and the content. The content is the anchor text.
@@ -60,7 +61,6 @@ const getMarkedAnchors = function( sentence, wordsRegex ) {
// Create a new anchor tag with a (marked) anchor text.
return reConstructAnchor( openTag, markedAnchorText );
} );
-
return { anchors, markedAnchors };
};
@@ -123,7 +123,6 @@ export function markWordsInSentences( wordsToMark, sentences, locale, matchWordC
sentences.forEach( function( sentence ) {
wordsFoundInSentence = matchWords( sentence, wordsToMark, locale, matchWordCustomHelper ).matches;
-
if ( wordsFoundInSentence.length > 0 ) {
markings = markings.concat( new Mark( {
original: sentence,