Syntax error: nothing to repeat

I can probably solve this another way but I would love to understand what I’m doing wrong here. I really appreciate if someone could explain it to me. Thanks in advance! :slight_smile:

Your code so far

function titleCase(str) {
let newArr = [];
const regex = /+w/;
str.split(" ").forEach(word => {
for (let i = 0; i < word.length; i++) {
  let newWord = "";
  if (i == 0 && word[i].match(regex)) {
    newWord + word[i].toUpperCase();
  } else {
    newWord + word[i].toLowerCase();

return newArr.join(" ");


titleCase("I'm a little tea pot");

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64; rv:84.0) Gecko/20100101 Firefox/84.0.

Challenge: Title Case a Sentence

Link to the challenge:

I’ll agree the error message is not as helpful as it could be. The problem is in your regex.

FYI, if you run your code through Node you get the following error:

SyntaxError: Invalid regular expression: /+w/: Nothing to repeat

If you don’t have Node installed on your computer I recommend you install it.

1 Like

what do you want to do with this?
if I try it in a regex tool ( it says “Pattern error”

Two things (mainly unrelated to the logic)

  1. SyntaxError: Invalid regular expression

  1. newWord + I assume you want some sort of assignment here not just concatenation that doesn’t get saved anywhere.

I would suggest you add some console.log statements. Make sure the data you are working with is what you expect it to be.

1 Like

I want to check if the character (word[i]) is alphabetic [a-z]… I don’t know if that make sense… :confused:

you can use [a-z], that is a valid character class that would match any of the letters (case sensitive in that case)

1 Like