Skip to content

Commit 192e0f0

Browse files
Regular Expression Consecutive duplicate words (#2032)
* Create script.js * Create README.md
1 parent 6a8fea3 commit 192e0f0

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Consecutive duplicate words
2+
3+
**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.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Consecutive Duplicate Words Detector
2+
// This regex finds repeated words that appear consecutively, such as "the the" or "and and".
3+
// Useful for grammar or text quality checks.
4+
5+
const duplicateWordsRegex = /\b([A-Za-z]+)\s+\1\b/gi;
6+
7+
function hasDuplicateWords(text) {
8+
return duplicateWordsRegex.test(text);
9+
}
10+
11+
// Example usage:
12+
console.log(hasDuplicateWords("This is the the example.")); // true
13+
console.log(hasDuplicateWords("We need to to check this.")); // true
14+
console.log(hasDuplicateWords("Everything looks good here.")); // false
15+
console.log(hasDuplicateWords("Hello hello world.")); // true (case-insensitive)
16+
console.log(hasDuplicateWords("No repetition found.")); // false

0 commit comments

Comments
 (0)