# Spot the bug - #91: Slug Formatter

**URL:** <https://forum.kirupa.com/t/spot-the-bug-91-slug-formatter/682677>\
**Category:** web dev\
**Created:** [July 18, 2026, 9:39pm UTC](https://forum.kirupa.com/t/spot-the-bug-91-slug-formatter/682677 "2026-07-18T21:39:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![HariSeldon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/hariseldon/32/31261_2.png) [@HariSeldon](https://forum.kirupa.com/u/HariSeldon)\
**Post date:** [July 18, 2026, 9:39pm UTC](https://forum.kirupa.com/t/spot-the-bug-91-slug-formatter/682677/1 "2026-07-18T21:39:52Z")

</div>

One regex bug in this formatter.

```js
function slugify(title) {
  return title.trim().toLowerCase().replace('/\s+/g', '-');
}

console.log(slugify('Hello World Again'));

```

Reply with what is broken and how you would fix it.

---

<div class="post-metadata">

**Author:** ![Yoshiii](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/yoshiii/32/31156_2.png) [@Yoshiii](https://forum.kirupa.com/u/Yoshiii)\
**Post date:** [July 19, 2026, 9:40pm UTC](https://forum.kirupa.com/t/spot-the-bug-91-slug-formatter/682677/2 "2026-07-19T21:40:13Z")

</div>

Your regex is a string literal, not a `RegExp`, so `. replace()` is looking for the exact characters `"/\s+/g"` instead of matching whitespace. Fix it by using a real regex (no quotes): `title. trim(). toLowerCase(). replace(/\s+/g, '-')`.

---

<div class="post-metadata">

**Author:** ![HariSeldon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/hariseldon/32/31261_2.png) [@HariSeldon](https://forum.kirupa.com/u/HariSeldon)\
**Post date:** [July 26, 2026, 3:00am UTC](https://forum.kirupa.com/t/spot-the-bug-91-slug-formatter/682677/3 "2026-07-26T03:00:10Z")

</div>

**Spot the Bug answer:** The regex is passed as a string ‘/\s+/g’ instead of an actual regex literal, so replace() only replaces the first literal occurrence of that exact string (which never matches).

**The fix:**

```js
return title.trim().toLowerCase().replace(/\s+/g, '-');

```

**Why:**  
String.prototype.replace treats a quoted ‘/\s+/g’ as a plain string to search for, not as a regular expression, so whitespace is never matched or replaced globally. Using an actual regex literal /\s+/g enables global whitespace matching and correct replacement.
