0

I am trying to use a global modifier to replace all instances of below but I keep on getting an error

Function

var chosenParticipants = newParticipants.replace(/<i class="fa fa-minus"></i></span>/g,'</strong>');

Error

Uncaught SyntaxError: Unexpected token <

2
  • 1
    You can often solve problems like this yourself by various debugging techniques. One that might work well here is "divide and conquer" or "bisecting". Type the regxp into the console and see what the error is. then, remove stuff from the regexp until the error goes away. The last thing you removed will be what caused the error. In this case, the error will go away when you remove the / in </i>. Hopefully that would give you a clue, or jog your memory, about the need to escape slashes. Commented May 5, 2016 at 4:15
  • 1
    More generally, thought, it is not recommended to manipulate HTML with regexps like this. There is a good chance you will break the HTML, or the regexp will apply somewhere you didn't expect, or the regexp approach will be hard to extend. Instead, use the various DOM routines that are available to manipulate the HTML. Commented May 5, 2016 at 4:16

1 Answer 1

3

The slashes in the regex need to be escaped by backslashes, otherwise the regex literal will be terminated at the first occurrence of / and an error will be thrown.

newParticipants.replace(/<i class="fa fa-minus"><\/i><\/span>/g, '</strong>');
                                                 ^^   ^^
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.