diff --git a/Specialized Areas/Regular Expressions/Consecutive duplicate words/README.md b/Specialized Areas/Regular Expressions/Consecutive duplicate words/README.md new file mode 100644 index 0000000000..a5907a0c3c --- /dev/null +++ b/Specialized Areas/Regular Expressions/Consecutive duplicate words/README.md @@ -0,0 +1,3 @@ +# Consecutive duplicate words + +**Overview** - This Regex can be used to find any two consecutive duplicate words. This can be mainly used in client scripts that validates naming conventions. diff --git a/Specialized Areas/Regular Expressions/Consecutive duplicate words/script.js b/Specialized Areas/Regular Expressions/Consecutive duplicate words/script.js new file mode 100644 index 0000000000..8bc0c4b975 --- /dev/null +++ b/Specialized Areas/Regular Expressions/Consecutive duplicate words/script.js @@ -0,0 +1,16 @@ +// Consecutive Duplicate Words Detector +// This regex finds repeated words that appear consecutively, such as "the the" or "and and". +// Useful for grammar or text quality checks. + +const duplicateWordsRegex = /\b([A-Za-z]+)\s+\1\b/gi; + +function hasDuplicateWords(text) { + return duplicateWordsRegex.test(text); +} + +// Example usage: +console.log(hasDuplicateWords("This is the the example.")); // true +console.log(hasDuplicateWords("We need to to check this.")); // true +console.log(hasDuplicateWords("Everything looks good here.")); // false +console.log(hasDuplicateWords("Hello hello world.")); // true (case-insensitive) +console.log(hasDuplicateWords("No repetition found.")); // false